PHP CURL 多線程操作代碼實(shí)例
更新時(shí)間:2015年05月13日 09:35:32 投稿:junjie
這篇文章主要介紹了PHP CURL 多線程操作代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
使用方法:
$urls = array("http://baidu.com", "http://21andy.com", "http://google.com");
$mp = new MultiHttpRequest($urls);
$mp->start();
/*
* Curl 多線程類
* 使用方法:
* ========================
$urls = array("http://baidu.com", "http://dzone.com", "http://google.com");
$mp = new MultiHttpRequest($urls);
$mp->start();
* ========================
*/
class MultiHttpRequest {
public $urls = array();
public $curlopt_header = 1;
public $method = "GET";
function __construct($urls = false) {
$this->urls = $urls;
}
function set_urls($urls) {
$this->urls = $urls;
return $this;
}
function is_return_header($b) {
$this->curlopt_header = $b;
return $this;
}
function set_method($m) {
$this->medthod = strtoupper($m);
return $this;
}
function start() {
if(!is_array($this->urls) or count($this->urls) == 0){
return false;
}
$curl = $text = array();
$handle = curl_multi_init();
foreach($this->urls as $k=>$v){
$curl[$k] = $this->add_handle($handle, $v);
}
$this->exec_handle($handle);
foreach($this->urls as $k=>$v){
curl_multi_getcontent($curl[$k]);
echo $curl[$k]."\n";
//$text[$k] = curl_multi_getcontent($curl[$k]);
//echo $text[$k], "\n\n";
curl_multi_remove_handle($handle, $curl[$k]);
}
curl_multi_close($handle);
}
private function add_handle($handle, $url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, $this->curlopt_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($handle, $curl);
return $curl;
}
private function exec_handle($handle) {
$flag = null;
do {
curl_multi_exec($handle, $flag);
} while ($flag > 0);
}
}
您可能感興趣的文章:
- PHP使用CURL_MULTI實(shí)現(xiàn)多線程采集的例子
- PHP Curl多線程原理實(shí)例詳解
- PHP使用CURL實(shí)現(xiàn)多線程抓取網(wǎng)頁
- php結(jié)合curl實(shí)現(xiàn)多線程抓取
- php中foreach結(jié)合curl實(shí)現(xiàn)多線程的方法分析
- php使用curl_init()和curl_multi_init()多線程的速度比較詳解
- 淺談php使用curl模擬多線程發(fā)送請(qǐng)求
- php curl批處理實(shí)現(xiàn)可控并發(fā)異步操作示例
- PHP curl批處理及多請(qǐng)求并發(fā)實(shí)現(xiàn)方法分析
- PHP中使用CURL發(fā)送get/post請(qǐng)求上傳圖片批處理功能
- php使用curl模擬多線程實(shí)現(xiàn)批處理功能示例
相關(guān)文章
ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說明
這篇文章主要介紹了ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說明,需要的朋友可以參考下2018-06-06
Laravel中ServiceProvider使用場(chǎng)景示例詳解
這篇文章主要為大家介紹了Laravel中ServiceProvider使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
php測(cè)試程序運(yùn)行速度和頁面執(zhí)行速度的代碼
microtime()函數(shù)返回當(dāng)前 Unix 時(shí)間戳的微秒數(shù)。用于檢測(cè)程序執(zhí)行時(shí)間的函數(shù),也是PHP內(nèi)置的時(shí)間函數(shù)之一,在PHP中可以用于對(duì)程序執(zhí)行時(shí)間的判斷,以及相同功能函數(shù)的執(zhí)行效率高低快慢的判斷。2022-12-12
php冒泡算法實(shí)現(xiàn)倒序和正序排列的示例代碼
冒泡排序是一種簡(jiǎn)單的排序算法,其主要思想是比較相鄰的兩個(gè)元素,根據(jù)需要交換位置,將較大(或較小)的元素逐漸冒泡到數(shù)組的一端,從而實(shí)現(xiàn)排序,這篇文章主要介紹了php冒泡算法實(shí)現(xiàn)倒序和正序排列,需要的朋友可以參考下2023-11-11
apache php mysql開發(fā)環(huán)境安裝教程
這篇文章主要為大家詳細(xì)介紹了apache php mysql開發(fā)環(huán)境安裝教程,感興趣的小伙伴們可以參考一下2016-07-07

