相信許多人對php手冊中語焉不詳的curl_multi一族的函數頭疼不已,它們文檔少,給的例子 更是簡單的讓你無從借鑒,我也曾經找了許多網頁,都沒見一個完整的應用例子。
curl_multi_add_handle
curl_multi_close
curl_multi_getcontent
#curl_multi_info_
curl_multi_getcontent#curl_multi_info_##i_read_
#curl_multi_remove_handlecurl_multi_select一般來說,想到要用這些函數時,目的顯然應該是要同時請求多個url,而不是一個依次請求,否則不如自己循環去調curl_exec好了。 步驟總結如下:
第一步:呼叫curl_multi_init
第二步:循環呼叫curl_multi_add_handle
這一步要注意的是,curl_multi_add_handle的第二個參數是由curl_init而來的子handle。
第三步:持續呼叫curl_multi_exec
第四步:根據需要循環呼叫curl_multi_getcontent取得結果
第五步:呼叫curl_multi_remove_handle,並為每個字handle呼叫curl_close
/* Here's a quick and dirty example for curl-multi from PHP, tested on PHP 5.0.0RC1 CLI / FreeBSD 5.2.1 */ $connomains = array( "http://www.cnn.com/", "http://www.canada.com/", "http://www.yahoo.com/" ); $mh = curl_multi_init(); foreach ($connomains as $i => $url) { $conn[$i]=curl_init($url); curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle ($mh,$conn[$i]); } do { $n=curl_multi_exec($mh,$active); } while ($active); foreach ($connomains as $i => $url) { $res[$i]=curl_multi_getcontent($conn[$i]); curl_close($conn[$i]); } print_r($res);
do { $mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active and $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } }
控制每個請求的超時時間,在curl_multi_add_handle之前透過curl_setopt去做:
class MultiCurl{ private $mch; private $timeout; private $urls; private $chs; private $errchs; private $content; public function __construct($urls, $timeout=300){ $this->mch = curl_multi_init(); $this->timeout = $timeout; $this->urls = $urls; $this->chs = array(); $this->errchs = array(); $this->content = array(); } public function ctlFlow(){ $this->createhandle(); $this->execurl(); $this->chs = array(); $this->errchs = array(); echo "<pre class="brush:php;toolbar:false">"; print_r($this->content); echo ""; return $this->content; } private function createhandle(){ if(!empty($this->urls)){ foreach($this->urls as $k => $url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_multi_add_handle($this->mch, $ch); $this->chs[$k] = $ch; } } } private function execurl(){ do { $mrc = curl_multi_exec($this->mch,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($this->mch) != -1) { usleep(300); do { $mrc = curl_multi_exec($this->mch, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } foreach($this->chs as $k => $c){ if (curl_error($c) == '') { $temp = curl_multi_getcontent($c); $count = 0; while(!$temp){ if($count > 3) break; usleep(100); $temp = curl_multi_getcontent($c); $count++; } $this->content[$k] = $temp; unset($this->chs[$k]); curl_multi_remove_handle($this->mch, $c); curl_close($c); } else { $this->errchs[$k] = $this->urls[$k]; } } } } $urlArr = array('baidu'=>'www.baidu.com', 'google'=>'www.google.com.hk', 'sogou'=>'www.sogou.com'); $mucurl = new MultiCurl($urlArr); $mucurl->ctlFlow();
以上是對PHP中curl_multi相關函數的應用講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!