$t=getTime();
$total=10;
for($i=0;$i<$total;$i++){
$url_arr[] = "http://www.111com.net /test";
}
$mh = curl_multi_init();
foreach ($url_arr as $i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1); // 设置返回页面输出内容
curl_multi_add_handle ($mh,$conn[$i]); // 添加线程
}
#----------------执行线程----------------
do {
$mrc = curl_multi_exec($mh,$active);
}while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active and $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($url_arr as $i => $url) {
$res[$i]=curl_multi_getcontent($conn[$i]);//得到页面输入内容
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
foreach($res as $k=>$v){
$k=str_pad($k,2,0);
echo "$k => $v ";
}
runTime($t);
#----------- calculate time function-------------
function getTime(){
$TIME=explode(" ",microtime());
$TIME=$TIME[1].substr($TIME[0],1);
return $TIME;
}
function runTime($t,$l=3){
$dif=getTime()-$t;
echo ' '.number_format($dif,$l);
}
?>
|