Jump to content
Sign in to follow this  
benphp

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

Recommended Posts

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);

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.