php curl批处理实现可控并发异步操作示例
发布时间:2021-05-16 13:33:46 所属栏目:编程 来源:网络整理
导读:本篇章节讲授php curl批处理赏罚实现可控并发异步操纵。供各人参考研究详细如下: 凡是环境下 PHP 中的 cURL 是阻塞运行的,就是说建设一个 cURL 哀求往后必需等它执行乐成可能超时才会执行下一个哀求:API接口会见一样平常会首选CURL 在现实项目可能本身编
副问题[/!--empirenews.page--]
本篇章节讲授php curl批处理赏罚实现可控并发异步操纵。分享给各人供各人参考,详细如下: 凡是环境下 PHP 中的 cURL 是阻塞运行的,就是说建设一个 cURL 哀求往后必需等它执行乐成可能超时才会执行下一个哀求:API接口会见一样平常会首选CURL 在现实项目可能本身编写小器材(好比消息聚合,商品价值监控,比价)的进程中,凡是必要从第3方网站可能API接口获取数据,在必要处理赏罚1个URL行列时,为了进步机能,可以回收cURL提供的 ';
print_r($response);
echo ' ' . date("Y-m-d H:i:s") . ' '; echo ' ' . str_repeat("-",100) . ' '; } $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','options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=sq&fenQuNum=41",array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime&referfrom=&rt=0.42521539455332336',//常人修真 'method' => 'POST','options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=frxz&fenQuNum=3",array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_gametime&referfrom=&rt=0.42521539455332336',//神魔仙界 'method' => 'POST','options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=smxj&fenQuNum=2",array( 'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type=rec_gametime&referfrom=&rt=0.42521539455332336',//倾世情缘 'method' => 'POST','options' => array( CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qsqy&fenQuNum=11",); 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(); 行使下来结果很好,没有副浸染,并发数可控,应用之处多多,本身施展想象吧
* @link http://t.qq.com/JustPzy
*/
/**
*单一的哀求工具
*/
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 );
}
}
/**
* 包括哀求排队处理赏罚
*/
class Curl {
/**
* 哀求url个数
* @var int
*/
private $size = 5;
/**
* 守候全部cURL批处理赏罚中的勾当毗连守候相应时刻
* @var int
*/
private $timeout = 5;
/**
* 完成哀求回调函数
* @var string
*/
private $callback = null;
/**
* cRUL设置
* @var array
*/
private $options = array (CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_CONNECTTIMEOUT => 30 );
/**
* 哀求头
* @var array
*/
private $headers = array ();
/**
* 哀求排队
* @var array
*/
private $requests = array ();
/**
* 哀求排队索引
* @var array
*/
private $request_map = array ();
/**
* 错误
* @var array
*/
private $errors = array ();
/**
* @access public
* @param string $callback 回调函数
* 该函数有4个参数($response,$request)
* $response url返回的body
* $info cURL毗连资源句柄的信息
* $error 错误
* $request 哀求工具
*/
public function __construct($callback = null) {
$this->callback = $callback;
}
/**
* 添加一个哀求工具到排队
* @access public
* @param object $request
* @return boolean
*/
public function add($request) {
$this->requests [] = $request;
return TRUE;
}
/**
* 建设一个哀求工具并添加到排队
* @access public
* @param string $url
* @param string $method
* @param string $post_data
* @param string $headers
* @param array $options
* @return boolean
*/
public function request($url,$options = null) {
$this->requests [] = new Curl_request ( $url,$method,$post_data,$headers,$options );
return TRUE;
}
/**
* 建设GET哀求工具
* @access public
* @param string $url
* @param string $headers
* @param array $options
* @return boolean
*/
public function get($url,$options = null) {
return $this->request ( $url,"GET",null,$options );
}
/**
* 建设一个POST哀求工具
* @access public
* @param string $url
* @param string $post_data
* @param string $headers
* @param array $options
* @return boolean
*/
public function post($url,"POST",$options );
}
/**
* 执行cURL
* @access public
* @param int $size 最大毗连数
* @return Ambigous (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |