php simple_html_dom load_file / file_get_contents超时无效

时间:2015-12-11 06:39:39

标签: php timeout simple-html-dom

我使用simple_html_dom解析html,以下是我的核心代码

set_time_limit(10000);
foreach ($urlList as $url) {
    ini_set('default_socket_timeout', 5);

    $context = stream_context_create(
        array(
            'http'=>array(
                'method' => 'GET', 
                'timeout' => 5
            ),
        )
    );
    $shd->load_file($url, false, $context);

    var_dump(0);

    $html = $shd->find("table");

    ...
}

但是它不适用于load_file()超时,只有超过10000秒才停止脚本set_time_limit(10000);

我希望load_file跳过下一个任务,当前任务超过5秒,是否有办法获得它?

1 个答案:

答案 0 :(得分:0)

最后我使用curl获取内容,然后使用simple_html_dom来处理内容。

function get_html_by_curl($url, $timeout = 5) {
     $ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

     $html = curl_exec($ch);     
     if (false === $html) {
         return false;
     }

     if (200 != curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
         return false;
     }

     return $html;
 }
 $content = get_html_by_curl('http://www.google.com', 5); $i = 0;
 while($i<3&&!$content) {
     $content = get_html_by_curl('http://www.google.com', 5);
      $i++; }

 if (false !== $html) {
     $shd->load($content ); 
}