遇到一个需要大循环中curl请求接口情况,循环请求接口特别耗时,
然后想着优化,curl中用curl_multi_init函数可实现批量请求,没成想优化是优化了,不顶用啊
发现少量请求时用curl_multi_ini是ok的,五条请求都返回成功,而五千请求就才成功了两百多条,而单纯的curl耗时长却都成功了
无所谓,实现了就好,最终结果都是为了确保在循环中请求都能成功。
其中用单纯的curl的关键在于,
兄台,自己看吧
public function multiCurl($url, $data) {
$queue = curl_multi_init();
foreach($data as $info) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $info);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_multi_add_handle($queue, $ch);
}
$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)) {
$results = curl_multi_getcontent($done['handle']);
$responses[] = $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 json_encode($responses);
}
public function file_get_contents_curl($url, $postData='', $header='',$options = []){
if(!extension_loaded('curl')) return 'curl extension not loaded';
$defaultOptions = [
'timeout' => 10
];
$options += $defaultOptions;
$ch = curl_init();
if($header){
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
}
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_TIMEOUT,$options['timeout']);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
if(!empty($postData)){//post传递
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
}
$str = curl_exec($ch);
$err = curl_error($ch);
if (false === $str || !empty($err)) {
$errno = curl_errno($ch);
curl_close($ch);
return 'Errno:' . $errno . ';ErrInfo:' . $err;
} else {
curl_close($ch);
return trim($str);
}
}
}