设置部分php脚本的超时时间

时间:2014-07-21 11:00:17

标签: php file

抱歉我的英文

我使用file_get_contents函数来获取" www.example.com"的HTML内容。 as string然后处理这个字符串。

我的问题是当这个网站花了很多时间打开时,我想为我的一部分脚本(不是所有脚本)设置时间,这样如果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

1 个答案:

答案 0 :(得分:1)

使用curl

 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;
}