PHP如何实现CURL多线程进行GETPOST请求?
- 内容介绍
- 文章标签
- 相关推荐
本文共计220个文字,预计阅读时间需要1分钟。
php PHP + CURL 多线程 GET/POST 请求
使用cURL进行多线程GET/POST请求,代码如下:
phpfunction curl($urls, $post_data=[]) { $mh=curl_multi_init(); $handles=[];
foreach ($urls as $url) { $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (!empty($post_data)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); } curl_multi_add_handle($mh, $ch); $handles[]=$ch; }
$running=null; do { curl_multi_exec($mh, $running); curl_multi_select($mh); } while ($running);
$results=[]; foreach ($handles as $ch) { $results[]=curl_multi_getcontent($ch); curl_multi_remove_handle($mh, $ch); curl_close($ch); }
curl_multi_close($mh); return $results;}
<?php /**************************************************************** PHP CURL 多线程 GET/POST curl(array('url?get=data','url'),array('','post_data')); *****************************************************************/ function curl($urls,$post) { $queue = curl_multi_init(); $map = array(); foreach ($urls as $key => $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post[$key]); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOSIGNAL, true); curl_multi_add_handle($queue, $ch); $map[(string) $ch] = $url; } $responses = array(); do { while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ; if ($code != CURLM_OK) { break; } while ($done = curl_multi_info_read($queue)) { $error = curl_error($done['handle']); $results = curl_multi_getcontent($done['handle']); $responses[$map[(string) $done['handle']]] = compact('error', 'results'); curl_multi_remove_handle($queue, $done['handle']); curl_close($done['handle']); } if ($active > 0) { curl_multi_select($queue, 0.5); } } while ($active); curl_multi_close($queue); return $responses; }
本文共计220个文字,预计阅读时间需要1分钟。
php PHP + CURL 多线程 GET/POST 请求
使用cURL进行多线程GET/POST请求,代码如下:
phpfunction curl($urls, $post_data=[]) { $mh=curl_multi_init(); $handles=[];
foreach ($urls as $url) { $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (!empty($post_data)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); } curl_multi_add_handle($mh, $ch); $handles[]=$ch; }
$running=null; do { curl_multi_exec($mh, $running); curl_multi_select($mh); } while ($running);
$results=[]; foreach ($handles as $ch) { $results[]=curl_multi_getcontent($ch); curl_multi_remove_handle($mh, $ch); curl_close($ch); }
curl_multi_close($mh); return $results;}
<?php /**************************************************************** PHP CURL 多线程 GET/POST curl(array('url?get=data','url'),array('','post_data')); *****************************************************************/ function curl($urls,$post) { $queue = curl_multi_init(); $map = array(); foreach ($urls as $key => $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post[$key]); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOSIGNAL, true); curl_multi_add_handle($queue, $ch); $map[(string) $ch] = $url; } $responses = array(); do { while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ; if ($code != CURLM_OK) { break; } while ($done = curl_multi_info_read($queue)) { $error = curl_error($done['handle']); $results = curl_multi_getcontent($done['handle']); $responses[$map[(string) $done['handle']]] = compact('error', 'results'); curl_multi_remove_handle($queue, $done['handle']); curl_close($done['handle']); } if ($active > 0) { curl_multi_select($queue, 0.5); } } while ($active); curl_multi_close($queue); return $responses; }

