Proper Handling of fopen function when site is down?
I am trying to properly handle fopen when the remote site or server is down..I "think" the script below is not handling it gracefully. It seems if the remote site is down, then the site that tries to run this script doesn't load as well...So what I am trying to do is prevent that somehow. How can I tell it to stop trying if the remote server takes too long?
if ($handle = @fopen('http://test.com/versions.xml','r')) {
$versions = fread($handle, 1024);
fclose($handle);
} elseif (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://test.com/versions.xml');
curl开发者_Python百科_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$versions = curl_exec($ch);
curl_close($ch);
}
Ok I found the answer to this...Simply do the following..
$context = stream_context_create(array('http'=>array('timeout'=>2)));
if ($handle = @fopen('http://test.com/versions.xml','r',false,$context))...
This will set the timeout to 2 seconds...
精彩评论