dpwo36915 2012-09-07 16:05
浏览 39
已采纳

PHP urlExists限制时间

Im doing a simple webapp that that is used to display some server's status, included the state of the server (ON/OFF). To check the server's status I use the funciotion "urlExists" so if it returns me a true i shot that server as online, offline in the other case.

When I check a server, if it is online the function executes fast, but when the server that i am checking is offline it takes too long to check the next one. So I am trying to limit the execution time of the function like this:

...
set_time_limit(1);
$exists = false;
$exists = urlExists($url);
set_time_limit();
...

but the execution time is the same using the set_time_limit(). I dont know if set_time_limit is used to do this kind of things but is the only thing i founded. Any idea on how I can do this ??

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • du060334 2012-09-07 16:23
    关注

    Try this function that uses CURL:

    function urlExists($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    
        $exists = curl_exec($ch);
        curl_close($ch);
    
        // If exec returns boolean, it's false, therefore the server is down/url doesn't exist.
        return !is_bool($exists);
    }
    

    As you can see, you can set a time limit for the connection with CURLOPT_CONNECTTIMEOUT. This way you don't need to set_time_limit();

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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据