为了账号安全,请及时绑定邮箱和手机立即绑定

php multi curl

标签:
PHP

<?php 

function multiCurl($res,$options=""){ 

if(count($res)<=0) return False; 

$handles = array(); 

if(!$options) // add default options 

$options = array( 

CURLOPT_HEADER=>0, 

CURLOPT_RETURNTRANSFER=>1, 

); 

// add curl options to each handle 

foreach($res as $k=>$row){ 

$ch{$k} = curl_init(); 

$options[CURLOPT_URL] = $row['url']; 

curl_setopt_array($ch{$k}, $options); 

$handles[$k] = $ch{$k}; 

$mh = curl_multi_init(); 

foreach($handles as $k => $handle){ 

curl_multi_add_handle($mh,$handle); 

//echo "<br>adding handle {$k}"; 

$running_handles = null; 

//execute the handles 

$cme=null;

$status=null;

do { 

$status_cme = curl_multi_exec($mh, $running_handles); 

} while ($cme == CURLM_CALL_MULTI_PERFORM); 

while ($running_handles && $status_cme == CURLM_OK) { 

if (curl_multi_select($mh) != -1) { 

do { 

$status_cme = curl_multi_exec($mh, $running_handles); 

// echo "<br>''threads'' running = {$running_handles}"; 

} while ($status == CURLM_CALL_MULTI_PERFORM); 

foreach($res as $k=>$row){ 

$res[$k]['error'] = curl_error($handles[$k]); 

if(!empty($res[$k]['error'])) 

$res[$k]['data']  = ''; 

else 

$res[$k]['data']  = curl_multi_getcontent( $handles[$k] );  // get results 

// close current handler 

curl_multi_remove_handle($mh, $handles[$k] ); 

curl_multi_close($mh); 

return $res; // return response 

$res = array( 

"11"=>array("url"=>"http://api.211.100.56.140.xip.io/shows.json/263?appKey=strKfLS2Hecp"), 

"12"=>array("url"=>"http://api.211.100.56.140.xip.io/shows.json/263?appKey=strKfLS2Hecp"), 

); 

print_r(multiCurl($res)); 

?>


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消