Jump to content

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


benphp

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

 

Link to comment
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.

Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.