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

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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制