Jump to content


Photo

Can't fetch external file - what am I doing wrong?


  • Please log in to reply
3 replies to this topic

#1 benphp

benphp
  • Members
  • PipPipPip
  • Advanced Member
  • 337 posts

Posted 13 July 2016 - 07:57 PM

I can use both file_get_contents and CURL to fetch the contents of a file on my server, but when I attempt to read a file from outside the server, both just spin forever, never reading the file.

 

I'm on IIS7 - and Curl is installed and running. I can browse to the locations with no trouble, but if I attempt to fetch the files using PHP, I get nothing. 

 

Is there some other setting in IIS that I need to enable or disable?

 

What am I missing? 

 

Neither works:

$sourceLink = "http://www.google.com";
$sourcePageHTML = file_get_contents($sourceLink);
print $sourcePageHTML;


$ch = curl_init("http://www.google.com");   
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
 
Both work:
$sourceLink = "http://myserver/myfile.php";
$sourcePageHTML = file_get_contents($sourceLink);
print $sourcePageHTML;


$ch = curl_init("http://myserver/myfile.php");   
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);

 



#2 Jacques1

Jacques1
  • Gurus
  • Turtles all the way down
  • 4,213 posts

Posted 13 July 2016 - 09:08 PM

What happens when you use cURL or wget on the command line without PHP involved?

 

Either way, you must have error handling when you call an external library. The fire-and-forget strategy doesn't work (as you can see). Check for errors, log the message, and I'm sure you'll get useful information.



#3 benphp

benphp
  • Members
  • PipPipPip
  • Advanced Member
  • 337 posts

Posted 13 July 2016 - 11:20 PM

CURL is a dll on my Windows server - I don't know how to run that from CMD - unless I download the standalone curl.exe.

 

If I wait half an hour or so I finally get an error from fie_get_contents:

 

Warning: file_get_contents(http://www.google.com) [ function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

 

My intuition is that there's some server or network setting that is denying the HTTP request from the server.


Edited by benphp, 13 July 2016 - 11:23 PM.


#4 Jacques1

Jacques1
  • Gurus
  • Turtles all the way down
  • 4,213 posts

Posted 14 July 2016 - 12:00 AM

My intuition is that there's some server or network setting that is denying the HTTP request from the server.

 

That's why I'm asking about the command line. I see no indication for a PHP problem, but this may very well be a network issue (e. g. a firewall which blocks outgoing HTTP requests).

 

Without concrete information, there's nothing I could do from here.

 

Check the firewall settings. Download a file from the command line (however this may work in Windows). When in doubt, use a packet sniffer to anaylze the exact network traffic.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users