<?php
class cUrlData{
function index(){
$list =array(1,2,3,4,5);
$urls = array(
<span style="white-space:pre"> </span>'localhost/mulity.php?act=test',
<span style="white-space:pre"> </span>'localhost/mulity.php?act=test'
) ;
print_r($this->async_get_url($list)); // [0] => example1, [1] => example2
}
// 并发请求函数
function async_get_url($url_array, $wait_usec = 0)
{
if (!is_array($url_array))
return false;
$wait_usec = intval($wait_usec);
$data = array('id'=>12);
$handle = array();
$running = 0;
$mh = curl_multi_init(); // multi curl handler
$i = 0;
foreach($url_array as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'localhost/mulity.php?act=test&id='.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return don't print
curl_setopt($ch, CURLOPT_POST, true); // 设置
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
curl_multi_add_handle($mh, $ch); // 把 curl resource 放进 multi curl handler 里
$handle[$i++] = $ch;
}
/* 执行 */
do {
curl_multi_exec($mh, $running);
if ($wait_usec > 0) /* 每个 connect 要间隔多久 */
usleep($wait_usec); // 250000 = 0.25 sec
} while ($running > 0);
/* 读取资料 */
foreach($handle as $i => $ch) {
$content = curl_multi_getcontent($ch);
$data[$i] = (curl_errno($ch) == 0) ? $content : false;
}
/* 移除 handle*/
foreach($handle as $ch) {
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
return $data;
}
<span style="font-family: 宋体, Arial;">public function test(){</span>
$data = isset($_GET['id']) ? $_GET['id'] : 110;
echo $data*2;
}
}
$m = new cUrlData;
$act = isset($_GET['act']) ? trim($_GET['act']) : 'index';
if (!(method_exists('cUrlData',$act))) {
echo "您输入的的地址不正确";
exit();
}
else $m->$act();
?>