PHP CURL实现并发多线程的抓取url地址

PHP CURL实现并发多线程,主要就是以:curl_multi_ * 的类库。一个简单的demo

另外感觉curl多线程没有想想那么的好,各位适应php的扩展 pthread

$urls = array(
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
    'http://localhost/test/time.php',
); // 设置要抓取的页面URL

set_time_limit(0);
$t1 = time();
$handle = curl_multi_init();
$curls = array();
foreach ($urls as $k=>$url)
{
    $curls[$k] = add_handle($handle, $url);
}
function add_handle(& $handle, $url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, '');
    curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //cookie
    curl_setopt($curl, CURLOPT_COOKIE, 'a=1');

    //post
    // curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_multi_add_handle($handle, $curl);
    return $curl;
}

$flag = null;
do {
    curl_multi_exec($handle, $flag);
} while ($flag > 0);

foreach($urls as $k=>$url){
    if(200 != curl_getinfo($curls[$k], CURLINFO_HTTP_CODE))
    {
        curl_multi_remove_handle($handle, $curls[$k]);
        continue;
    }
    $content = curl_multi_getcontent ($curls[$k]) ;
    echo $content."<br>";

    curl_multi_remove_handle($handle, $curls[$k]);
}
curl_multi_close($handle);
$t2 = time();
echo $t2-$t1;



上一篇: 高并发下LNMP的构架考虑及资源分配简单思考 
下一篇: 微信支付在某些情况下加载请求非常慢的问题分析和解决办法

目前还没有人评论,您发表点看法?
发表评论

评论内容 (必填):