drkxgs9358 2011-06-08 21:05
浏览 12
已采纳

站点停机时正确处理fopen功能?

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_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $versions = curl_exec($ch);
    curl_close($ch);    
}
  • 写回答

1条回答 默认 最新

  • dpus81500574 2011-06-08 23:12
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?