【问题标题】:Set time out for part of php script [duplicate]为部分php脚本设置超时[重复]
【发布时间】:2014-09-11 20:44:33
【问题描述】:

对不起我的英语

我使用file_get_contents函数获取“www.example.com”的HTML内容作为字符串,然后处理这个字符串。

我的问题是当这个网站需要很长时间才能打开时,我想为我的部分脚本(不是所有脚本)设置超时时间,这样如果file_get_contents 函数需要很长时间,我的脚本就会停止它并继续下一行。 我正在使用 php5 是否可以这样做,我感谢您的所有建议

示例:

//set time out =  10S
$siteStr = @file_get_contents("www.example.com");
//10S is finished so stop file_get_contents and continue

【问题讨论】:

    标签: php file


    【解决方案1】:

    使用卷曲

     function get_data($url) {
        $ch = curl_init();
        $timeout = 10;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    

    【讨论】:

    • 抱歉,我的服务器上没有激活 curl 模块,我无权激活它。我必须使用 file_get_content
    • 所以看看 cmets,10x
    • 谢谢,我在另一个问题stackoverflow.com/questions/10236166/… 中找到了解决方案
    猜你喜欢
    • 2014-05-09
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多