I have an issue with a URL my application is trying to access timing out. I am trying to catch this timeout and to solve this problem am using this code:
$timeout = 120;
if(false == $handle = @fsockopen($host, $port, $errno, $errstr, $timeout))
{
throw new Exception("Could not connect to url: ".$errstr);
}
$getRequest = "GET {$url} HTTP/1.0\r\n";
$getRequest .= "Host: {$urlParts['host']}\r\n";
$getRequest .= "Connection: close\r\n\r\n";
fwrite($handle, $getRequest);
$maxExecutionTime = ini_get('max_execution_time');
set_time_limit($timeout+10);
stream_set_timeout($handle, $timeout);
$head = fread($handle, 1024); // Read the first 1024 bytes
if($maxExecutionTime == 0) {
$maxExecutionTime = 30;
}
set_time_limit($maxExecutionTime);
$stream_metadata = stream_get_meta_data($handle);
if($stream_metadata['timed_out'] === true) {
throw new Exception("Connection timed out");
}
My URL I am using for the timeout is behind a firewall, so I can not share it, but it is designed to sleep() for 5 minutes. When I try to run this code, execution stops at $head = fread($handle, 1024); and after 90 seconds I get a 500 error from the server "Premature end of script". When I investigate at the debug level in the apache logs I see:
[Fri Aug 26 11:10:45 2011] [warn] [client 192.168.10.202] mod_fcgid: read timeout from pipe
[Fri Aug 26 11:10:45 2011] [error] [client 192.168.10.202] Premature end of script headers: validateUrl.php
with 'validateUrl.php' being the url I am accessing this script through. I'm not sure increasing FcgidIOTimeout to a higher value is safe, as it would apply to all of my pages. Any ideas/ comments?
System details: PHP Version 5.2.13 running on Windows NT EPG-WEB 5.2 build 3790 Apache: Apache/2.2.19 (Win32) mod_fcgid/2.3.6