tags:

views:

341

answers:

2

I have a script with line:

$id = strtolower(implode('',file($ip_service . $ip)));

When executed the file function will be like file(http://www.url.com/filename.php?119.160.120.38)

On server A it works fine but on Server B it gives following error:

file(http://www.url.com/filename.php?xxx.xxx.xxx.xxx) [function.file]: failed to open stream: Connection timed out in /home/path/filename.php on line 22

Line 22 is the above line of code.

Server A has PHP 4.4.6, Server B has 4.4.8

Any help will be highly appreciated.

+1  A: 

Your server couldn't establish a tcp/ip connection to the server hosting www.url.com in a given time period (20 seconds? 30 seconds? What ever the default is or what you have specified as timeout). The other server didn't even reject the connection actively, there just wasn't any response at all. Could be e.g. a firewall issue where some or all of your or the other server's packets where silently dropped.

VolkerK
+3  A: 

Using file() and/or file_get_contents() for accessing files across the internet is fairly error prone. For example, I don't think they follow redirects. The timeout period is also very short, not the typical network timeout. It's also difficult to do error capture to see why it failed. I always use CURL for accessing files over the network. It takes a few more lines of code, but is much more reliable. Note, PHP CURL support may not be installed in your setup.

$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/filename.php?119.160.120.38');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if ( $result==false ) {
  echo curl_errno($ch).' '.curl_error($ch);
}
curl_close($ch);

Obviously you would want to do something beside echo out the error message if there was an error. The $result variable will have the contents of the file if it succeeds.

Brent Baisley
as a side note: php (or the curl extension) can be compiled to register curl as handler for url wrappers. There's some #ifdef PHP_CURL_URL_WRAPPERS ... php_register_url_stream_wrapper(... in curl/interface.c but I have absolutely no idea if it's used or not ;-)
VolkerK