PHP中多线程抓取网页

用php自带的curl功能实现的多线程下载工具,比file_get_contents,以及linux自带的命令行curl、wget效率高多了。

大家如果觉得好,就拿去直接用吧。

/**
 * @param mixed string or array,参数$urlArray是要抓取的网页(或文件,下同)的网址,可以是单个网址,也可以是多个网址组成的数组。
 */
function multiDownload($urlArray) {
    if (empty($urlArray)) return false;
    
    $isStr = false;
    if (is_string($urlArray)) {
        $urlArray = array($urlArray);
        $isStr = true;
    }
    
    self::log(sprintf("%s Multi thread download begin...", __METHOD__));
    
    $mh = curl_multi_init(); //curl_multi_init --  Returns a new cURL multi handle
    $curlArray = array(); 

    foreach ($urlArray as $i => $url) { 
        self::log(sprintf("%s Download url: |%s|...", __METHOD__, $url));

        $curlArray[$i] = curl_init($url); 

        curl_setopt($curlArray[$i], CURLOPT_RETURNTRANSFER, true); //设置为true表示返回抓取的内容,而不是直接输出到浏览器上。TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly

        curl_setopt($curlArray[$i], CURLOPT_AUTOREFERER, true); //自动设置referer。TRUE to automatically set the Referer: field in requests where it follows a Location: redirect.

        curl_setopt($curlArray[$i], CURLOPT_FOLLOWLOCATION, true); //跟踪url的跳转,比如301, 302等

        curl_setopt($curlArray[$i], CURLOPT_MAXREDIRS, 2); //跟踪最大的跳转次数

        curl_setopt($curlArray[$i], CURLOPT_HEADER, 0); //TRUE to include the header in the output. 

        curl_setopt($curlArray[$i], CURLOPT_ENCODING, ""); //接受的编码类型,The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent. 

        curl_setopt($curlArray[$i], CURLOPT_CONNECTTIMEOUT, 5); //连接超时时间         

        curl_multi_add_handle($mh, $curlArray[$i]); //curl_multi_add_handle --  Add a normal cURL handle to a cURL multi handle 
    }
     
    $running = NULL;
    $count = 0;
    do { 
        //10秒钟没退出,就超时退出
        if ($count++>100) break; 
        usleep(100000); 
        curl_multi_exec($mh, $running); //curl_multi_exec --  Run the sub-connections of the current cURL handle 
    } while($running > 0);
    
    $content = array(); 
    foreach ($urlArray as $i => $url) {
        $content[$url] = curl_multi_getcontent($curlArray[$i]); //curl_multi_getcontent --  Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
    } 
    
    //curl_multi_remove_handle --  Remove a multi handle from a set of cURL handles
    foreach ($urlArray as $i => $url){ 
        curl_multi_remove_handle($mh, $curlArray[$i]); 
    }
    
    //curl_multi_close --  Close a set of cURL handles 
    curl_multi_close($mh);
    
    self::log(sprintf("%s Multi thread download end...", __METHOD__));
    
    //如果参数$urlArray是字符串,则将返回值也转换为字符串
    if ($isStr) $content = implode('', $content);
    
    return $content;
}


上一篇: asp正则验证是否汉字的三种Pattern的区别
下一篇: PHP正则验证中文
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1932
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑