码迷,mamicode.com
首页 > Web开发 > 详细

rolling_curl curl批量采集函数留份

时间:2015-04-03 15:04:21      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

摘自:http://www.searchtb.com/2012/06/rolling-curl-best-practices.html

 1 function rolling_curl($urls, $delay=0) {
 2             $queue = curl_multi_init();
 3                 $map = array();
 4           
 5             foreach ($urls as $url) {
 6                 $ch = curl_init();
 7           
 8                 curl_setopt($ch, CURLOPT_URL, $url);
 9                 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
10                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
11                 curl_setopt($ch, CURLOPT_HEADER, 0);
12                 curl_setopt($ch, CURLOPT_NOSIGNAL, true);
13           
14                 curl_multi_add_handle($queue, $ch);
15                 $map[(string) $ch] = $url;
16             }
17           /*主要内容*/
18             $responses = array();
19             do {
20                 while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
21           
22                 if ($code != CURLM_OK) { break; }
23           
24                 // a request was just completed -- find out which one
25                 while ($done = curl_multi_info_read($queue)) {
26           
27                     // get the info and content returned on the request
28                     $info = curl_getinfo($done[‘handle‘]);
              //可以通过info的信息来判断是否连接成功
29 $error = curl_error($done[‘handle‘]); 30 // $results = callback(curl_multi_getcontent($done[‘handle‘]), $delay);
              //curl_multi_getcontent($done[‘handle‘]) 已经可以获取到详细内容,即可以开始业务逻辑
31 $responses[$map[(string) $done[‘handle‘]]] = compact(‘info‘, ‘error‘, ‘results‘); 32 33 // remove the curl handle that just completed 34 curl_multi_remove_handle($queue, $done[‘handle‘]); 35 curl_close($done[‘handle‘]); 36 } 37 38 // Block for data in / output; error handling is done by curl_multi_exec 39 if ($active > 0) { 40 curl_multi_select($queue, 0.5); 41 } 42 43 } while ($active); 44 /*主要内容*/
45 curl_multi_close($queue); 46 return $responses; 47  } 48 49 50 }

 

rolling_curl curl批量采集函数留份

标签:

原文地址:http://www.cnblogs.com/lxdd/p/4389920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!