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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章