一般CURL 抓网页的方法, 是一页一页抓, 假设要抓 4页, 所费时间各别是 5,10,7,5 秒, 那全部总合所花的时间就是 5 + 10 + 7 + 5 = 27 秒。若能同时间去抓取多个网页, 所花费的时间 5,10,7,5 秒, 全部总合所花的时间是 10 秒。(花费最多时间的秒数)
于javascript 可使用 ajax 的 async(yahoo.util.connect.asyncrequest)来达成, 于 php 可以用 curl 来达成此 multi-threading 的效果。
程序(async.php)
|
以下为引用的内容: |
使用
|
以下为引用的内容: $urls = array('http://example1.com', 'http://example2.com'); print_r(async_get_url($urls)); // [0] => example1, [1] => example2 ?> |
测试
sleep.php # 看时间延长取得的效果
立即学习“PHP免费学习笔记(深入)”;
|
以下为引用的内容: sleep(intval($_GET['time'])); echo intval($_GET['time']); ?> |
|
以下为引用的内容: $url_array = array( 'http://example.com/sleep.php?time=5', 'http://example.com/sleep.php?time=10', 'http://example.com/sleep.php?time=7', 'http://example.com/sleep.php?time=5', ); print_r(async_get_url($url_array)); // 总花费时间会是 10 秒, 并印出 [0] => 5, [1] => 10, [2] => 7, [3] => 5 ?> |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号