0

I want to put the content of a URL in a string and the process it. However, I have a problem.

The url is http://138.201.123.244:8990/time/data.txt

When I use cURL and file_get_contents in my server (cPanel), I did not receive any content!

What's the problem?

Mohammad
  • 127
  • 2
  • 16
  • Can you open the url in a browser? Because I can't – NineBerry Apr 19 '17 at 17:07
  • Show us your curl/file_get_contents code. – ceejayoz Apr 19 '17 at 17:16
  • You might be getting a 'connection refused' , did you set curl to Verbose? – Forbs Apr 19 '17 at 17:22
  • Could be any number of problems. Does your environment have cURL installed? Is cURL version correct for use with PHP? Are there networking problems between your server and that endpoint? Is that endpoint returning 200 response? – Mike Brant Apr 19 '17 at 17:43
  • @ceejayoz `$file = "http://138.201.123.244:8990/time/data.txt"; $out_result = file_get_contents($file);` – Mohammad Apr 19 '17 at 19:09
  • @NineBerry Yes, works fine. – Mohammad Apr 19 '17 at 19:10
  • @MikeBrant Yes, I tried many methods but I did not answer. – Mohammad Apr 19 '17 at 19:11
  • @Mohammad But "Did not answer means" what? Your server could not reach the target site (i.e. some sort of network issue)? Your server can reach the target site, but your request is timing out? Your the target server returns unexpected response? There are too many possible problems here to give you an answer without you narrowing it down with steps you have taken to investigate the problem. – Mike Brant Apr 19 '17 at 21:52
  • @MikeBrant I get 200 response. I've tested code another server but result is the same. – Mohammad Apr 20 '17 at 07:13
  • @Mohammad Then is sounds like target endpoint is the problem. – Mike Brant Apr 20 '17 at 20:19

1 Answers1

0

In case this isn't a configuration problem with your server this code should work:

<?PHP
    $my_URL="http://138.201.123.244:8990/time/data.txt";

    $lines = file($my_URL);

    foreach ($lines as $line_num => $line) 
    {
            echo $line_num." ".$line;
    }
?>
Hexodus
  • 12,361
  • 6
  • 53
  • 72
  • Thanks for the answer. You code result: `Warning: file(http://138.201.123.244:8990/time/data.txt): failed to open stream: Connection timed out in /home/pishroar/public_html/bots/t.php on line 4 Warning: Invalid argument supplied for foreach() in /home/pishroar/public_html/bots/t.php on line 6` – Mohammad Apr 19 '17 at 18:57
  • @Mohammad I tested this code as well and it works as expected. It outputs a single line with this base64 image. The Invalid argument is a result of the timeout. You should increase the max_execution_time in your php.ini or .htaccess to prevent this timeout. See: http://stackoverflow.com/questions/8744107/increase-max-execution-time-in-php – Hexodus Apr 20 '17 at 13:17
  • @Mohammad I'm sorry to hear that. I think that this is mainly a server timeout problem you've got there. Maybe you could adjust this question and mention the timeout so someone wiser then my could answer you. – Hexodus Apr 22 '17 at 13:22