脚本宝典收集整理的这篇文章主要介绍了php curl批处理实现可控并发异步操作示例,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了PHP curl批处理实现可控并发异步操作。分享给大家供大家参考,具体如下:
通常情况下 PHP 中的 cURL 是阻塞运行的,就是说创建一个 cURL 请求以后必须等它执行成功或者超时才会执行下一个请求:API接口访问一般会首选CURL
在实际项目或者自己编写小工具(比如新闻聚合,商品价格监控,比价)的过程中,通常需要从第3方网站或者API接口获取数据,在需要处理1个URL队列时,为了提高性能,可以采用cURL提供的curl_multi_*
族函数实现简单的并发.
<
PRe class="
brush:
PHp;">
<>
PHP
include 'curl.class.
PHP';
function callback($response,$info,$error,$request)
{
echo 'response:
';
print_r($response);
echo '
' . date("Y-m-d H:i:s") . '
';
echo '
' . str_re
PEat("-",100) . '
';
}
$USER_COOKIE = (!empty(
$_REQUEST['cookie'])) ? $_REQUEST['cookie'] :
file_get_contents("cookie.txt");
$curl = new Curl ("callback");
$data = array(
array(
'url' => 'http://dyactive2.vip.xunl
ei.COM/com_sign/?g
ame=qmr&type=rec_gametime&refer
From=&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&referf
rom=&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",);
for
each ($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->dis
play_errors();
脚本宝典总结
以上是脚本宝典为你收集整理的php curl批处理实现可控并发异步操作示例全部内容,希望文章能够帮你解决php curl批处理实现可控并发异步操作示例所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。