时间:2021-07-01 10:21:17 帮助过:1人阅读
for ($i=0; $i < 100; $i++)
{
$urls[] = "http://blog.csdn.net/lg_lin?wd=".mt_rand(10000,20000);
}
$t = microtime(true);
//单线程foreach ($urlsas$key => $value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $value);
$response = curl_exec($ch);
curl_close($ch);
$result_new[$key] = $response;
}
var_dump($result_new);
$e = microtime(true);
echo"For循环:".($e-$t)."\n";
/**
**** 多线程****
* 由于受网络带宽限制,一次性不能并发太多,可以分批处理
* 总量超过100以后,效果就比单线程明显提高
*/$p = 5;//每次执行多少条$ring = ceil(count($urls) / $p);
for($n = 0; $n < $ring; $n++)
{
$temp_url = array();
$star = $n * $p;
$end = ($n+1) * $p;
for($ii = $star; $ii < $end; $ii++)
{
if (isset($urls[$ii]))
{
$temp_url[] = $urls[$ii];
}
}
$mh = curl_multi_init();
foreach ($temp_urlas$i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i], CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($conn[$i], CURLOPT_TIMEOUT, 30);
curl_setopt($conn[$i], CURLOPT_URL, $url);
curl_multi_add_handle($mh,$conn[$i]);
}
do{
curl_multi_exec($mh, $active);
} while ($active);
$active = null;
foreach ($temp_urlas$i => $url) {
$data[$i] = curl_multi_getcontent($conn[$i]); // 获得爬取的代码字符串 }
foreach ($temp_urlas$i => $url) {
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
var_dump($data);
}
$t = microtime(true);
echo"多线程:".($t-$e)."\n";
exit();
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了利用curl_multi_init并行多线程处理网络数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。