php curl批處理實(shí)現(xiàn)可控并發(fā)異步操作示例
本文實(shí)例講述了php curl批處理實(shí)現(xiàn)可控并發(fā)異步操作。分享給大家供大家參考,具體如下:
通常情況下 PHP 中的 cURL 是阻塞運(yùn)行的,就是說(shuō)創(chuàng)建一個(gè) cURL 請(qǐng)求以后必須等它執(zhí)行成功或者超時(shí)才會(huì)執(zhí)行下一個(gè)請(qǐng)求:API接口訪問(wèn)一般會(huì)首選CURL
在實(shí)際項(xiàng)目或者自己編寫小工具(比如新聞聚合,商品價(jià)格監(jiān)控,比價(jià))的過(guò)程中, 通常需要從第3方網(wǎng)站或者API接口獲取數(shù)據(jù), 在需要處理1個(gè)URL隊(duì)列時(shí), 為了提高性能, 可以采用cURL提供的curl_multi_*
族函數(shù)實(shí)現(xiàn)簡(jiǎn)單的并發(fā).
<?php include 'curl.class.php'; function callback($response, $info, $error, $request) { echo 'response:<br>'; print_r($response); echo '<br>' . date("Y-m-d H:i:s") . ' <br>'; echo '<br>' . str_repeat("-", 100) . '<br>'; } $USER_COOKIE = (!empty($_REQUEST['cookie'])) ? $_REQUEST['cookie'] : file_get_contents("cookie.txt"); $curl = new Curl ("callback"); $data = array( array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qmr&type=rec_gametime&referfrom=&rt=0.42521539455332336', //秦美人 'method' => 'POST', 'post_data' => '', 'header' => null, 'options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qmr&fenQuNum=3", CURLOPT_COOKIE => $USER_COOKIE, ) ), array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=sq&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神曲 'method' => 'POST', 'post_data' => '', 'header' => null, 'options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=sq&fenQuNum=41", CURLOPT_COOKIE => $USER_COOKIE, ) ), array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime&referfrom=&rt=0.42521539455332336', //凡人修真 'method' => 'POST', 'post_data' => '', 'header' => null, 'options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=frxz&fenQuNum=3", CURLOPT_COOKIE => $USER_COOKIE, ) ), array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神魔仙界 'method' => 'POST', 'post_data' => '', 'header' => null, 'options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=smxj&fenQuNum=2", CURLOPT_COOKIE => $USER_COOKIE, ) ), array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type=rec_gametime&referfrom=&rt=0.42521539455332336', //傾世情緣 'method' => 'POST', 'post_data' => '', 'header' => null, 'options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qsqy&fenQuNum=11", CURLOPT_COOKIE => $USER_COOKIE, ) ), ); foreach ($data as $val) { $request = new Curl_request ($val ['url'], $val ['method'], $val ['post_data'], $val ['header'], $val ['options']); $curl->add($request); } $curl->execute(); echo $curl->display_errors();
使用下來(lái)效果很好,沒(méi)有副作用,并發(fā)數(shù)可控,應(yīng)用之處多多,自己發(fā)揮想象吧
<?php /** * cURL批量處理 工具類 * * @since Version 1.0 * @author Justmepzy <justmepzy@gmail.com> * @link http://t.qq.com/JustPzy */ /** *單一的請(qǐng)求對(duì)象 */ class Curl_request { public $url = ''; public $method = 'GET'; public $post_data = null; public $headers = null; public $options = null; /** * * @param string $url * @param string $method * @param string $post_data * @param string $headers * @param array $options * @return void */ public function __construct($url, $method = 'GET', $post_data = null, $headers = null, $options = null) { $this->url = $url; $this->method = strtoupper( $method ); $this->post_data = $post_data; $this->headers = $headers; $this->options = $options; } /** * @return void */ public function __destruct() { unset ( $this->url, $this->method, $this->post_data, $this->headers, $this->options ); } } /** * 包含請(qǐng)求列隊(duì)處理 */ class Curl { /** * 請(qǐng)求url個(gè)數(shù) * @var int */ private $size = 5; /** * 等待所有cURL批處理中的活動(dòng)連接等待響應(yīng)時(shí)間 * @var int */ private $timeout = 5; /** * 完成請(qǐng)求回調(diào)函數(shù) * @var string */ private $callback = null; /** * cRUL配置 * @var array */ private $options = array (CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_CONNECTTIMEOUT => 30 ); /** * 請(qǐng)求頭 * @var array */ private $headers = array (); /** * 請(qǐng)求列隊(duì) * @var array */ private $requests = array (); /** * 請(qǐng)求列隊(duì)索引 * @var array */ private $request_map = array (); /** * 錯(cuò)誤 * @var array */ private $errors = array (); /** * @access public * @param string $callback 回調(diào)函數(shù) * 該函數(shù)有4個(gè)參數(shù)($response,$info,$error,$request) * $response url返回的body * $info cURL連接資源句柄的信息 * $error 錯(cuò)誤 * $request 請(qǐng)求對(duì)象 */ public function __construct($callback = null) { $this->callback = $callback; } /** * 添加一個(gè)請(qǐng)求對(duì)象到列隊(duì) * @access public * @param object $request * @return boolean */ public function add($request) { $this->requests [] = $request; return TRUE; } /** * 創(chuàng)建一個(gè)請(qǐng)求對(duì)象并添加到列隊(duì) * @access public * @param string $url * @param string $method * @param string $post_data * @param string $headers * @param array $options * @return boolean */ public function request($url, $method = 'GET', $post_data = null, $headers = null, $options = null) { $this->requests [] = new Curl_request ( $url, $method, $post_data, $headers, $options ); return TRUE; } /** * 創(chuàng)建GET請(qǐng)求對(duì)象 * @access public * @param string $url * @param string $headers * @param array $options * @return boolean */ public function get($url, $headers = null, $options = null) { return $this->request ( $url, "GET", null, $headers, $options ); } /** * 創(chuàng)建一個(gè)POST請(qǐng)求對(duì)象 * @access public * @param string $url * @param string $post_data * @param string $headers * @param array $options * @return boolean */ public function post($url, $post_data = null, $headers = null, $options = null) { return $this->request ( $url, "POST", $post_data, $headers, $options ); } /** * 執(zhí)行cURL * @access public * @param int $size 最大連接數(shù) * @return Ambigous <boolean, mixed>|boolean */ public function execute($size = null) { if (sizeof ( $this->requests ) == 1) { return $this->single_curl (); } else { return $this->rolling_curl ( $size ); } } /** * 單個(gè)url請(qǐng)求 * @access private * @return mixed|boolean */ private function single_curl() { $ch = curl_init (); $request = array_shift ( $this->requests ); $options = $this->get_options ( $request ); curl_setopt_array ( $ch, $options ); $output = curl_exec ( $ch ); $info = curl_getinfo ( $ch ); // it's not neccesary to set a callback for one-off requests if ($this->callback) { $callback = $this->callback; if (is_callable ( $this->callback )) { call_user_func ( $callback, $output, $info, $request ); } } else return $output; return true; } /** * 多個(gè)url請(qǐng)求 * @access private * @param int $size 最大連接數(shù) * @return boolean */ private function rolling_curl($size = null) { if ($size) $this->size = $size; else $this->size = count($this->requests); if (sizeof ( $this->requests ) < $this->size) $this->size = sizeof ( $this->requests ); if ($this->size < 2) $this->set_error ( 'size must be greater than 1' ); $master = curl_multi_init (); //添加cURL連接資源句柄到map索引 for($i = 0; $i < $this->size; $i ++) { $ch = curl_init (); $options = $this->get_options ( $this->requests [$i] ); curl_setopt_array ( $ch, $options ); curl_multi_add_handle ( $master, $ch ); $key = ( string ) $ch; $this->request_map [$key] = $i; } $active = $done = null; do { while ( ($execrun = curl_multi_exec ( $master, $active )) == CURLM_CALL_MULTI_PERFORM ) ; if ($execrun != CURLM_OK) break; //有一個(gè)請(qǐng)求完成則回調(diào) while ( $done = curl_multi_info_read ( $master ) ) { //$done 完成的請(qǐng)求句柄 $info = curl_getinfo ( $done ['handle'] );// $output = curl_multi_getcontent ( $done ['handle'] );// $error = curl_error ( $done ['handle'] );// $this->set_error ( $error ); //調(diào)用回調(diào)函數(shù),如果存在的話 $callback = $this->callback; if (is_callable ( $callback )) { $key = ( string ) $done ['handle']; $request = $this->requests [$this->request_map [$key]]; unset ( $this->request_map [$key] ); call_user_func ( $callback, $output, $info, $error, $request ); } curl_close ( $done ['handle'] ); //從列隊(duì)中移除已經(jīng)完成的request curl_multi_remove_handle ( $master, $done ['handle'] ); } //等待所有cURL批處理中的活動(dòng)連接 if ($active) curl_multi_select ( $master, $this->timeout ); } while ( $active ); //完成關(guān)閉 curl_multi_close ( $master ); return true; } /** * 獲取沒(méi)得請(qǐng)求對(duì)象的cURL配置 * @access private * @param object $request * @return array */ private function get_options($request) { $options = $this->__get ( 'options' ); if (ini_get ( 'safe_mode' ) == 'Off' || ! ini_get ( 'safe_mode' )) { $options [CURLOPT_FOLLOWLOCATION] = 1; $options [CURLOPT_MAXREDIRS] = 5; } $headers = $this->__get ( 'headers' ); if ($request->options) { $options = $request->options + $options; } $options [CURLOPT_URL] = $request->url; if ($request->post_data && strtolower($request->method) == 'post' ) { $options [CURLOPT_POST] = 1; $options [CURLOPT_POSTFIELDS] = $request->post_data; } if ($headers) { $options [CURLOPT_HEADER] = 0; $options [CURLOPT_HTTPHEADER] = $headers; } return $options; } /** * 設(shè)置錯(cuò)誤信息 * @access public * @param string $msg */ public function set_error($msg) { if (! empty ( $msg )) $this->errors [] = $msg; } /** * 獲取錯(cuò)誤信息 * @access public * @param string $open * @param string $close * @return string */ public function display_errors($open = '<p>', $close = '</p>') { $str = ''; foreach ( $this->errors as $val ) { $str .= $open . $val . $close; } return $str; } /** * @access public * @param string $name * @param string $value * @return boolean */ public function __set($name, $value) { if ($name == 'options' || $name == 'headers') { $this->{$name} = $value + $this->{$name}; } else { $this->{$name} = $value; } return TRUE; } /** * * @param string $name * @return mixed * @access public */ public function __get($name) { return (isset ( $this->{$name} )) ? $this->{$name} : null; } /** * @return void * @access public */ public function __destruct() { unset ( $this->size, $this->timeout, $this->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors ); } } // END Curl Class /* End of file curl.class.php */
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php多進(jìn)程并發(fā)編程防止出現(xiàn)僵尸進(jìn)程的方法分析
- PHP高并發(fā)和大流量解決方案整理
- PHP 并發(fā)場(chǎng)景的幾種解決方案
- PHP下用Swoole實(shí)現(xiàn)Actor并發(fā)模型的方法
- php多進(jìn)程模擬并發(fā)事務(wù)產(chǎn)生的問(wèn)題小結(jié)
- PHP利用Mysql鎖解決高并發(fā)的方法
- PHP curl批處理及多請(qǐng)求并發(fā)實(shí)現(xiàn)方法分析
- PHP使用curl_multi實(shí)現(xiàn)并發(fā)請(qǐng)求的方法示例
- 詳解PHP服務(wù)器如何在有限的資源里最大提升并發(fā)能力
相關(guān)文章
PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫,修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結(jié)合實(shí)例形式總結(jié)分析了php針對(duì)文件與目錄的獲取、運(yùn)算、打開(kāi)、創(chuàng)建、讀取、寫入、修改、刪除、判斷等常見(jiàn)操作技巧,需要的朋友可以參考下2016-09-09The specified CGI application misbehaved by not returning a
The specified CGI application misbehaved by not returning a complete set of HTTP headers2011-03-03有關(guān)JSON以及JSON在PHP中的應(yīng)用
簡(jiǎn) 單地說(shuō),JSON 可以將 JavaScript 對(duì)象中表示的一組數(shù)據(jù)轉(zhuǎn)換為字符串,然后就可以在函數(shù)之間輕松地傳遞這個(gè)字符串,或者在異步應(yīng)用程序中將字符串從 Web 客戶機(jī)傳遞給服務(wù)器端程序。2010-04-04php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例
這篇文章主要介紹了php的mssql數(shù)據(jù)庫(kù)連接類,以一個(gè)類實(shí)例的形式演示了PHP實(shí)現(xiàn)針對(duì)mssql數(shù)據(jù)庫(kù)的各種常用操作方法,包括對(duì)數(shù)據(jù)庫(kù)的連接與增刪改查等操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11PHP定時(shí)任務(wù)延緩執(zhí)行的實(shí)現(xiàn)
這篇文章主要介紹了PHP定時(shí)任務(wù)延緩執(zhí)行的實(shí)現(xiàn),很簡(jiǎn)單,但很實(shí)用,需要的朋友可以參考下面的示例2014-10-10php?substr()去掉最后一位字符的實(shí)例方法
在本篇文章里小編給大家整理了一篇關(guān)于php?substr()去掉最后一位字符的實(shí)例方法,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-12-12解析:php調(diào)用MsSQL存儲(chǔ)過(guò)程使用內(nèi)置RETVAL獲取過(guò)程中的return值
本篇文章是對(duì)php調(diào)用MsSQL存儲(chǔ)過(guò)程使用內(nèi)置RETVAL獲取過(guò)程中的return值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07