这两天有一客户定制了一个免登录发布模块,因为在模块中需要涉及到很多图片下载的问题,考虑到性能问题,所以特别写了一个CURL_MULTI远程采集网页的函数,以方便以后使用,估计以后都不会使用原来的单线程curl函数去foreach了,其性能对比很明显的。同样获取我的博客的十个不同网页,curl_multi:4.5246081352234,file_get_contents:33.001797914505,将近8倍的效率,可想而知,如果在附件更多的情况下,性能差异就越明显了,希望对您有所帮助!
<?php
$text = remote(array('http://www.zzvips.com/','http://www.baidu.com/'));
print_r($text);
function remote($urls) {
if (!is_array($urls) or count($urls) == 0) {
return false;
}
$curl = $text = array();
$handle = curl_multi_init();
foreach($urls as $k => $v) {
$nurl[$k]= preg_replace('~([^:\/\.]+)~ei', "rawurlencode('\\1')", $v);
$curl[$k] = curl_init($nurl[$k]);
curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl[$k], CURLOPT_HEADER, 0);
curl_multi_add_handle ($handle, $curl[$k]);
}
$active = null;
do {
$mrc = curl_multi_exec($handle, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($handle) != -1) {
do {
$mrc = curl_multi_exec($handle, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($curl as $k => $v) {
if (curl_error($curl[$k]) == "") {
$text[$k] = (string) curl_multi_getcontent($curl[$k]);
}
curl_multi_remove_handle($handle, $curl[$k]);
curl_close($curl[$k]);
}
curl_multi_close($handle);
return $text;
}