php curl 并发请求

protected static function mCurl($urlArray){
	$mh = curl_multi_init();
	$chArray = [];
	foreach($urlArray as $url){
		$chArray[] = $ch = curl_init(strpos($url,'/')===0?API_URL.$url:$url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_multi_add_handle($mh, $ch);
	}
	curl_multi_exec($mh, $still_running);
	while($still_running){
		curl_multi_select($mh);
		curl_multi_exec($mh, $still_running);
	}
	$rsArray = [];
	foreach($chArray as $ch){
		$response = curl_multi_getcontent($ch);
		self::log(curl_getinfo($ch,CURLINFO_EFFECTIVE_URL)."\r\n".$response);
		$rsArray[] = json_decode($response, true);
		curl_multi_remove_handle($mh, $ch);
	}
	curl_multi_close($mh);
	return $rsArray;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注