This article mainly introduces the implementation of PHP using CURL_MULTI implementation of Concurrent request Method Example, has a certain reference value, now share to everyone, the need for friends can refer to
The examples in this article describe how PHP implements concurrent requests using Curl_multi. Share to everyone for your reference, as follows:
Class Curlmultiutil {/** * Gets the Curl Request object according to Url,postdata, this is relatively simple, you can see the official document */private static function Getcurlobject ($url, $pos Tdata=array (), $header =array ()) {$options = array (); $url = Trim ($url); $options [Curlopt_url] = $url; $options [Curlopt_timeout] = 3; $options [Curlopt_returntransfer] = true; foreach ($header as $key = = $value) {$options [$key] = $value; } if (!empty ($postData) && Is_array ($postData)) {$options [curlopt_post] = true; $options [Curlopt_postfields] = Http_build_query ($postData); } if (Stripos ($url, ' https ') = = = 0) {$options [curlopt_ssl_verifypeer] = false; } $ch = Curl_init (); Curl_setopt_array ($ch, $options); return $ch; }/** * [request description] * @param [type] $chList * @return [Type] */private static function request ($chLis T) {$downloader = Curl_multi_init (); Put three requested objects into the downloader foreach ($chList as $ch) {Curl_multi_add_handle ($downloader, $ch); } $res = Array (); Polling Do {while ($execrun = Curl_multi_exec ($downloader, $running)) = = = Curlm_call_multi_perform); if ($execrun! = CURLM_OK) {break; }//Once a request has been completed, find out, handle, because curl is the bottom layer is select, so the maximum is limited by the 1024x768 while ($done = Curl_multi_info_read ($downloader)) {//From Request for information, content, error//$info = Curl_getinfo ($done [' handle ']); $output = curl_multi_getcontent ($done [' handle ']); $error = Curl_error ($done [' handle ']); $res [] = $output; The request has been completed Curl handle Delete curl_multi_remove_handle ($downloader, $done [' handle ']); }//When there is no data to jam, the use of the CPU to hand out, to avoid the above do dead loop empty data caused by the CPU 100% if ($running) {$rel = Curl_multi_select ($downl Oader, 1); if ($rel = =-1) {usleep (1000); }} if ($running = = false) {break; }}while (True); Curl_multi_close ($downloader); return $res; }/** * [get description] * @param [type] $URLARR * @return [Type] */public static function get ($URLARR) { $data = Array (); if (!empty ($URLARR)) {$chList = array (); foreach ($urlArr as $key = + $url) {$chList [] = Self::getcurlobject ($url); } $data = Self::request ($chList); } return $data; }}