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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解