curl_multi_init

curl_multi_init比curl_init发送100个请求,快非常多!具体细节如下。

1 https源码

[php]  view plain  copy
  1. <?php  
  2. /* 
  3.  * https.php 
  4.  * 
  5.  * https请求类 http://blog.csdn.net/CleverCode 
  6.  * 
  7.  * modification history: 
  8.  * -------------------- 
  9.  * 2016/5/25, by CleverCode, Create 
  10.  * 
  11.  */  
  12. class Https  
  13. {/*{{{*/  
  14.   
  15.     /** 
  16.      * https 发起post请求 
  17.      *  
  18.      * @param string $url url信息 
  19.      * @param mixed $data 参数信息[$data = '{"a":1,"b":2}' or $data = array("a" => 1,"b" => 2)] 
  20.      * @param int $timeOut 超时设置 
  21.      * @param string $proxyHost 代理host 
  22.      * @param int $proxyPort 代理端口 
  23.      * @return string 
  24.      */  
  25.     public static function post($url$data = null,$timeOut = 20, $proxyHost = null, $proxyPort = null)  
  26.     {/*{{{*/  
  27.         try {  
  28.             if (strlen($url) < 1) {  
  29.                 return null;  
  30.             }  
  31.   
  32.             $ch = curl_init();  
  33.   
  34.             // 设置url  
  35.             curl_setopt($ch, CURLOPT_URL, $url);  
  36.   
  37.             if(false == empty($data))  
  38.             {  
  39.                 curl_setopt($ch, CURLOPT_POST, 1);   
  40.                 // array  
  41.                 if (is_array($data) && count($data) > 0)   
  42.                 {  
  43.                     curl_setopt($ch, CURLOPT_POST, count($data));                  
  44.                 }  
  45.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  46.             }  
  47.   
  48.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  49.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  50.   
  51.             // 如果成功只将结果返回,不自动输出返回的内容  
  52.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  53.             // user-agent  
  54.             curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");  
  55.             // 超时  
  56.             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);  
  57.   
  58.             // 使用代理  
  59.             if (strlen($proxyHost) > 0 && strlen($proxyPort) > 0) {  
  60.                 // 代理认证模式  
  61.                 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);  
  62.                 // 代理服务器地址  
  63.                 curl_setopt($ch, CURLOPT_PROXY, $proxyHost);  
  64.                 // 代理服务器端口  
  65.                 curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);  
  66.                 // 使用http代理模式  
  67.                 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);  
  68.             }  
  69.   
  70.             // 执行  
  71.             $out = curl_exec($ch);  
  72.             // 关闭  
  73.             curl_close($ch);  
  74.             return $out;  
  75.         } catch ( Exception $e ) {  
  76.             return null;  
  77.         }  
  78.   
  79.         return null;  
  80.     }/*}}}*/  
  81.   
  82.     /** 
  83.      * https 发起post多发请求 
  84.      *  
  85.      * @param array $nodes url和参数信息。$nodes = array 
  86.      *                                              ( 
  87.      *                                                 [0] = > array 
  88.      *                                                   ( 
  89.      *                                                       'url' => 'http://www.baidu.com', 
  90.      *                                                       'data' => '{"a":1,"b":2}' 
  91.      *                                                   ), 
  92.      *                                                 [1] = > array 
  93.      *                                                   ( 
  94.      *                                                       'url' => 'http://www.baidu.com', 
  95.      *                                                       'data' => null 
  96.      *                                                   ) 
  97.      *                                                 .... 
  98.      *                                              ) 
  99.      * @param int $timeOut 超时设置 
  100.      * @return array   
  101.      */  
  102.     public static function postMulti($nodes,$timeOut = 5)  
  103.     {/*{{{*/  
  104.         try   
  105.         {  
  106.             if (false == is_array($nodes))   
  107.             {  
  108.                 return array();  
  109.             }  
  110.   
  111.             $mh = curl_multi_init();   
  112.             $curlArray = array();  
  113.             foreach($nodes as $key => $info)  
  114.             {  
  115.                 if(false == is_array($info))  
  116.                 {  
  117.                     continue;  
  118.                 }  
  119.                 if(false == isset($info['url']))  
  120.                 {  
  121.                     continue;  
  122.                 }  
  123.   
  124.                 $ch = curl_init();  
  125.                 // 设置url  
  126.                 $url = $info['url'];  
  127.                 curl_setopt($ch, CURLOPT_URL, $url);  
  128.   
  129.                 $data = isset($info['data']) ? $info['data'] :null;  
  130.                 if(false == empty($data))  
  131.                 {  
  132.                     curl_setopt($ch, CURLOPT_POST, 1);   
  133.                     // array  
  134.                     if (is_array($data) && count($data) > 0)   
  135.                     {  
  136.                         curl_setopt($ch, CURLOPT_POST, count($data));                  
  137.                     }  
  138.                     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  139.                 }  
  140.   
  141.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  142.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  143.                 // 如果成功只将结果返回,不自动输出返回的内容  
  144.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  145.                 // user-agent  
  146.                 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");  
  147.                 // 超时  
  148.                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);  
  149.                   
  150.                 $curlArray[$key] = $ch;  
  151.                 curl_multi_add_handle($mh$curlArray[$key]);   
  152.             }  
  153.   
  154.             $running = NULL;   
  155.             do {   
  156.                 usleep(10000);   
  157.                 curl_multi_exec($mh,$running);   
  158.             } while($running > 0);   
  159.   
  160.             $res = array();   
  161.             foreach($nodes as $key => $info)   
  162.             {   
  163.                 $res[$key] = curl_multi_getcontent($curlArray[$key]);   
  164.             }   
  165.             foreach($nodes as $key => $info){   
  166.                 curl_multi_remove_handle($mh$curlArray[$key]);   
  167.             }   
  168.             curl_multi_close($mh);       
  169.             return $res;  
  170.         }   
  171.         catch ( Exception $e )   
  172.         {  
  173.             return array();  
  174.         }  
  175.   
  176.         return array();  
  177.     }/*}}}*/  
  178.   
  179. }/*}}}*/  


2  100个请求

2.1 循环调用https::post()一百次。

[php]  view plain  copy
  1. <?php  
  2.   
  3. require_once "https.php";  
  4.   
  5. function microtimeFloat()                     
  6. {/*{{{*/                                              
  7.     list($usec$sec) = explode(" ", microtime());    
  8.     return ($usec + $sec);                            
  9. }/*}}}*/                                              
  10.   
  11. $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=abcdasdfasdfasdfasdfasdfasdfasdfasdfasdf';  
  12.   
  13. $startTime = microtimeFloat();  
  14. $count = 100;  
  15. for($i=1; $i <= 100;$i++)  
  16. {  
  17.     $res = Https::post($url);  
  18. }  
  19. $endTime = microtimeFloat();  
  20.   
  21. echo $endTime - $startTime;  
  22. echo "\r\n";  
  23.   
  24. ?>  

2.2 调用https::postMulti()一次发100个url。

[php]  view plain  copy
  1. <?php  
  2.   
  3. require_once "https.php";  
  4.   
  5. function microtimeFloat()                     
  6. {/*{{{*/                                              
  7.     list($usec$sec) = explode(" ", microtime());    
  8.     return ($usec + $sec);                            
  9. }/*}}}*/                                              
  10.   
  11. $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=abcdasdfasdfasdfasdfasdfasdfasdfasdfasdf';  
  12.   
  13. $nodes = array();  
  14. for($i=1; $i <= 100;$i++)  
  15. {  
  16.     $info = array();  
  17.     $info['url'] = $url;  
  18.     $nodes[] = $info;  
  19. }  
  20.   
  21. $startTime = microtimeFloat();  
  22. $res = Https::post($nodes);  
  23. $endTime = microtimeFloat();  
  24.   
  25. echo $endTime - $startTime;  
  26. echo "\r\n";  
  27.   
  28. ?>  

3 结果对比

2.1 方式耗时:15.47503900528。

2.2 方式耗时:0.49252104759216。

15.47503900528 /  0.49252104759216 = 30。

2.2 比2.1快30倍!!!

4 结论

   当需要一次发送多个请求的时候,尽量使用curl_multi_init方式。

猜你喜欢

转载自blog.csdn.net/m0_37827630/article/details/78060989