php 使用 curl_multi 方法批量获取网页内容
<?php
/*
php 使用 curl_multi 方法批量获取网页内容
*/
function curl_multi_get($urls = array()){
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
curl_setopt($conn[$i], CURLOPT_HEADER ,0);
curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,true);
curl_multi_add_handle ($mh,$conn[$i]);
}
do {
curl_multi_exec($mh,$active);
} while ($active);
foreach ($urls as $i => $url) {
$data[$i] = curl_multi_getcontent($conn[$i]);
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
return $data;
}
$urls=array();
$urls[]='http://www.baidu.com';
$urls[]='http://www.php100.com';
$urls[]='http://www.163.com/';
$data=curl_multi_get($urls);
var_dump($data);