curl_multi异步并发请求http
<?php
$start_time=date("h:i:sa");
$urls=[];
for ($i=0; $i <10 ; $i++) {
$urls[]="http://www.downxia.com/downinfo/2315".$i.".html";
}
//var_dump($urls);
// GetTitle('klasjdkla<title>313asds12</title>');
rolling_curl($urls,'GetTitle');
function GetTitle($output){
preg_match('/<title>.*<\/title>/i',$output,$matches);
var_dump($matches[0]);
}
$end_time=date("h:i:sa");
echo '开始时间是:'.$start_time;
echo '结束时间是:'.$end_time;
function rolling_curl($urls, $callback, $custom_options = null){
//多个url访问
// make sure the rolling window isn't greater than the # of urls确保滚动窗口不大于#的url
$rolling_window = 5;
$rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;
$master = curl_multi_init();
$curl_arr = array();
// add additional curl options here添加额外的旋度的选择
$std_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5
);
$options = ($custom_options) ? ($std_options + $custom_options) : $std_options;
// start the first batch of requests开始第一批请求
for ($i = 0; $i < $rolling_window; $i++) {
$ch = curl_init();
$options[CURLOPT_URL] = $urls[$i];
curl_setopt_array($ch, $options);
curl_multi_add_handle($master, $ch);
}
do {
//$master =curl_multi_init()句柄
//$running 用来判断操作是否仍在执行的标识的引用,必须的参数
while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
//该函数仅返回关于整个批处理栈相关的错误。即使返回 CURLM_OK 时单个传输仍可能有问题。
if ($execrun != CURLM_OK) {
break;
}
// a request was just completed -- find out which one一个请求是刚刚完成,找出哪一个
while ($done = curl_multi_info_read($master)) {
$info = curl_getinfo($done['handle']);
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']);
// request successful. process output using the callback function.
//请求成功。使用回调函数流程输出。
$callback($output);
// start a new request (it's important to do this before removing the old one)
//开始一个新的请求(之前做这件事是很重要的删除旧的)
// 当$i等于$urls数组大小时不用再增加了
if($i<sizeof($urls)){
$ch= curl_init();
$options[CURLOPT_URL] = $urls[$i++]; // increment i
curl_setopt_array($ch, $options);
curl_multi_add_handle($master, $ch);
}
// remove the curl handle that just completed
curl_multi_remove_handle($master, $done['handle']);
} else {
// request failed. add error handling.请求失败。添加错误处理。
}
}
} while ($running);
curl_multi_close($master);
return true;
}