test.php
<?php
function multiCurl($res, $options=""){
if(count($res)<=0) return False;
$handles = array();
if(!$options) // add default options
$options = array(
CURLOPT_HEADER=>0,
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_TIMEOUT=>1,//超时时间
);
// add curl options to each handle
foreach($res as $k=>$row){
$ch{$k} = curl_init();
$options[CURLOPT_URL] = $row['url'];
$opt = curl_setopt_array($ch{$k}, $options);
var_dump($opt);
$handles[$k] = $ch{$k};
}
$mh = curl_multi_init();
// add handles
foreach($handles as $k => $handle){
$err = curl_multi_add_handle($mh, $handle);
}
$running_handles = null;
do {
curl_multi_exec($mh, $running_handles);
curl_multi_select($mh);
} while ($running_handles > 0);
foreach($res as $k=>$row){
$res[$k]['error'] = curl_error($handles[$k]);
if(!empty($res[$k]['error']))
$res[$k]['data'] = '';
else
$res[$k]['data'] = curl_multi_getcontent( $handles[$k] ); // get results
// close current handler
curl_multi_remove_handle($mh, $handles[$k] );
}
curl_multi_close($mh);
return $res; // return response
}
$res = array(
"0"=>array("url"=>"http://localhost/curl/test1.php"),
"1"=>array("url"=>"http://localhost/curl/test2.php"),
"2"=>array("url"=>"http://localhost/curl/test3.php"),
);
echo "<pre>";
print_r( multiCurl($res));
echo "</pre>";
?>
test1.php
<?php
sleep(1);
echo test1;
?>
test2.php
<?php
sleep(2);
echo test2;
?>
test3.php
<?php
sleep(3);
echo test3;
?>
已有 0 条评论