PHP使用curl_multi併發請求

class CurlMultiUtil {
    /**
    * 根據url,postData獲取curl請求對象,這個比較簡單,能夠看官方文檔
    */
    private static function getCurlObject($url,$postData=array(),$header=array()){
        $options = array();
        $url = trim($url);
        $options[CURLOPT_URL] = $url;
        $options[CURLOPT_TIMEOUT] = 3;
        $options[CURLOPT_RETURNTRANSFER] = true;
        foreach($header as $key=>$value){
            $options[$key] =$value;
        }
        if(!empty($postData) && is_array($postData)){
            $options[CURLOPT_POST] = true;
            $options[CURLOPT_POSTFIELDS] = http_build_query($postData);
        }
        if(stripos($url,'https') === 0){
            $options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        $ch = curl_init();
        curl_setopt_array($ch,$options);
        return $ch;
    }
    /**
     * [request description]
     * @param  [type] $chList
     * @return [type]
     */
    private static function request($chList){
        $downloader = curl_multi_init();
        // 將三個待請求對象放入下載器中
        foreach ($chList as $ch){
            curl_multi_add_handle($downloader,$ch);
        }
        $res = array();
        // 輪詢
        do {
            while (($execrun = curl_multi_exec($downloader, $running)) == CURLM_CALL_MULTI_PERFORM);
            if ($execrun != CURLM_OK) {
                break;
            }
            // 一旦有一個請求完成,找出來,處理,由於curl底層是select,因此最大受限於1024
            while ($done = curl_multi_info_read($downloader)){
                // 從請求中獲取信息、內容、錯誤
                // $info = curl_getinfo($done['handle']);
                $output = curl_multi_getcontent($done['handle']);
                // $error = curl_error($done['handle']);
                $res[] = $output;
                // 把請求已經完成了得 curl handle 刪除
                curl_multi_remove_handle($downloader, $done['handle']);
            }
            // 當沒有數據的時候進行堵塞,把 CPU 使用權交出來,避免上面 do 死循環空跑數據致使 CPU 100%
            if ($running) {
                $rel = curl_multi_select($downloader, 1);
                if($rel == -1){
                    usleep(1000);
                }
            }
            if($running == false){
                break;
            }
        }while(true);
        curl_multi_close($downloader);
        return $res;
    }
    /**
     * [get description]
     * @param  [type] $urlArr
     * @return [type]
     */
    public static function get($urlArr){
        $data = array();
        if (!empty($urlArr)) {
            $chList = array();
            foreach ($urlArr as $key => $url) {
                $chList[] = self::getCurlObject($url);
            }
            $data = self::request($chList);
        }
        return $data;
    }
}
相關文章
相關標籤/搜索