PHP Curl Batch control concurrent asynchronous operations

Source: Internet
Author: User
Tags call back
This time for everyone to bring PHP Curl Batch control concurrent asynchronous operation, PHP Curl Batch control concurrency asynchronous operation of the attention to what, the following is the actual case, together to see.

Typically, curl in PHP is blocked, meaning that a curl request must be made after it succeeds or times out to execute the next Request: API interface Access generally prefers CURL

In the actual project or to write their own gadgets (such as news aggregation, commodity price monitoring, parity) process, usually need to get data from the 3rd party Web site or API interface, in order to improve performance when processing 1 URL queues, you can use the family functions provided by curl to curl_multi_* achieve simple concurrency.

<?phpinclude ' curl.class.php '; function callback ($response, $info, $error, $request) {echo ' response:<br> '; Print_r ($response); Echo ' <br> '. Date ("Y-m-d h:i:s"). ' <br> '; Echo ' <br> '. Str_repeat ("-", 100). ' <br> ';} $USER _cookie = (!empty ($_request[' COOKIE "))? $_request[' cookie ': file_get_contents ("Cookie.txt"), $curl = new Curl ("callback"), $data = array (' URL ' + ' HT tp://dyactive2.vip.xunlei.com/com_sign/?game=qmr&type=rec_gametime&referfrom=&rt=   0.42521539455332336 ',//Qinmei ' method ' = ' ' POST ', ' post_data ' + ', ' header ' = null, ' options ' = = Array ( Curlopt_referer = "Http://niu.xunlei.com/entergame/?gameNo=qmr&fenQuNum=3", Curlopt_cookie = $USER _ Cookies,)), array (' url ' = = ' http://dyactive2.vip.xunlei.com/com_sign/?game=sq&type=rec_gametime& referfrom=&rt=0.42521539455332336 ',//Divine Comedy ' method ' = ' POST ', ' post_data ' + ', ' header ' and ' = null ', ' option  s ' = = Array ( Curlopt_referer = "Http://niu.xunlei.com/entergame/?gameNo=sq&fenQuNum=41", Curlopt_cookie = $USER _ Cookies,)), array (' url ' = = ' http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime& referfrom=&rt=0.42521539455332336 ',//Mortal fix ' method ' = ' POST ', ' post_data ' = ', ' header ' and ' = null, ' Opti ONS ' = = Array (curlopt_referer = "Http://niu.xunlei.com/entergame/?gameNo=frxz&fenQuNum=3", Curlopt_ COOKIE = $USER _cookie,)), array (' url ' = = ' http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_  gametime&referfrom=&rt=0.42521539455332336 ',//Magic celestial ' method ' = ' POST ', ' post_data ' = ', ' header ' = ' NULL, ' options ' = = Array (curlopt_referer = "http://niu.xunlei.com/entergame/?gameNo=smxj&fenQuNum=2", CU Rlopt_cookie = $USER _cookie,)), array (' url ' = = ' Http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type =rec_gametime&referfrom=&rt=0.42521539455332336 ',//Pour world love ' method ' = ' POST ', ' post_data ' + ', ' header ' = null, ' options ' = = Array (curlopt_referer = "Http://niu.xunlei.com/entergame/?gameNo=qsqy&fenQuNum=11", Curlopt_cookie = $USER _cookie,));  foreach ($data as $val) {$request = new curl_request ($val [' url '], $val [' method '], $val [' Post_data '], $val [' Header '], $val [' Options ']; $curl->add ($request);} $curl->execute (); Echo $curl->display_errors ();

use down the effect is very good, no side effects, concurrency can be controlled, a lot of applications, their own imagination to play it

<?php/** * Curl Bulk Processing Tool class * * @since Version 1.0 * @author justmepzy <justmepzy@gmail.com> * @link http://t.qq.com /justpzy *//** * Single Request object */class curl_request {public $url = ' ', public $method = ' GET ', public $post _data = null; PU Blic $headers = null; public $options = null; /** * * @param string $url * @param string $method * @param string $post _data * @param string $headers * @param arr Ay $options * @return void */Public function construct ($url, $method = ' GET ', $post _data = null, $headers = NULL, $opti  ONS = null) {$this->url = $url;  $this->method = Strtoupper ($method);  $this->post_data = $post _data;  $this->headers = $headers; $this->options = $options; }/** * @return void */Public Function destruct () {unset ($this->url, $this->method, $this->post_data, $thi S->headers, $this->options); }}/** * contains request queue processing */class Curl {/** * request URL number * @var int */Private $size = 5;/** * waits for active connection waiting response time in all Curl batches * @va  R int*/Private $timeout = 5; /** * Completion Request callback function * @var String */private $callback = null; /** * Crul Configuration * @var Array */private $options = Array (curlopt_ssl_verifypeer = 0,curlopt_returntransfer = 1, Curlopt_connecttimeout = 30); /** * Request Header * @var array */private $headers = Array (); /** * Request Queue * @var Array */private $requests = Array (); /** * Request Queue Index * @var array */private $request _map = Array (); /** * ERROR * @var array */private $errors = Array ();   /** * @access public * @param string $callback callback function * This function has 4 parameters ($response, $info, $error, $request) * $response The body returned by the URL * $info Curl Connection Resource handle information * $ERROR Error * $request Request Object */Public function construct ($callback = null) {$this->call back = $callback; }/** * Add a Request object to the queue * @access public * @param Object $request * @return Boolean */Public Function Add ($request) {$  this->requests [] = $request; return TRUE; }/** * Creates a request object and adds it to the queue * @access public * @param string $url * @param StRing $method * @param string $post _data * @param string $headers * @param array $options * @return Boolean * public function request ($url, $method = ' GET ', $post _data = null, $headers = NULL, $options = null) {$this->requests [] = NE  W curl_request ($url, $method, $post _data, $headers, $options); return TRUE; /** * Create GET Request Object * @access public * @param string $url * @param string $headers * @param array $options * @return b Oolean */Public function get ($url, $headers = null, $options = null) {return $this->request ($url, "get", NULL, $h Eaders, $options); /** * Create a POST Request object * @access public * @param string $url * @param string $post _data * @param string $headers * @pa Ram Array $options * @return Boolean */Public function post ($url, $post _data = null, $headers = NULL, $options = NULL) {return $this->request ($url, "POST", $post _data, $headers, $options);} /** * Perform curl * @access public * @param int $size Maximum number of connections * @return ambigous <booleaN, Mixed>|boolean */Public Function execute ($size = null) {if (sizeof ($this->requests) = = 1) {return $this  ->single_curl ();  } else {return $this->rolling_curl ($size);  }}/** * Single URL request * @access private * @return Mixed|boolean */Private Function Single_curl () {$ch = Curl_init ();  $request = Array_shift ($this->requests);  $options = $this->get_options ($request);  Curl_setopt_array ($ch, $options);  $output = curl_exec ($ch);  $info = Curl_getinfo ($ch);   It ' s not neccesary to set a callback for one-off requests if ($this->callback) {$callback = $this->callback;   if (is_callable ($this->callback)) {Call_user_func ($callback, $output, $info, $request);  }} else return $output; return true;  }/** * Multiple URL requests * @access private * @param int $size Maximum number of connections * @return Boolean */Private Function rolling_curl ($size =  NULL) {if ($size) $this->size = $size; else $this->size = count ($this->requesTS);  if (sizeof ($this->requests) < $this->size) $this->size = sizeof ($this->requests);  if ($this->size < 2) $this->set_error (' size must be greater than 1 ');  $master = Curl_multi_init ();   Add a Curl connection resource handle to the map index for ($i = 0; $i < $this->size; $i + +) {$ch = Curl_init ();   $options = $this->get_options ($this->requests [$i]);   Curl_setopt_array ($ch, $options);   Curl_multi_add_handle ($master, $ch);   $key = (string) $ch;  $this->request_map [$key] = $i;  } $active = $done = null;   Do {while ($execrun = Curl_multi_exec ($master, $active)) = = = Curlm_call_multi_perform);   if ($execrun! = CURLM_OK) break; There is a request to complete the callback while ($done = Curl_multi_info_read ($master)) {//$done the completed request handle $info = Curl_getinfo ($done [' H    Andle ']);//$output = Curl_multi_getcontent ($done [' handle ']);//$error = Curl_error ($done [' handle ']);//    $this->set_error ($error); Call the callback function if it exists $callback = $this->callback;     if (is_callable ($callback)) {$key = (string) $done [' Handle '];     $request = $this->requests [$this->request_map [$key]];     unset ($this->request_map [$key]);    Call_user_func ($callback, $output, $info, $error, $request);    } curl_close ($done [' handle ']);   Remove the completed request Curl_multi_remove_handle ($master, $done [' handle ') from the queue;  }//waits for all activities in the curl batch to connect if ($active) curl_multi_select ($master, $this->timeout);  } while ($active);  Finish closing curl_multi_close ($master); return true; /** * Gets the curl configuration of the no Request object * @access Private * @param object $request * @return Array */Private function get_options ($r  Equest) {$options = $this->get (' options ');   if (Ini_get (' safe_mode ') = = ' Off ' | |! ini_get (' Safe_mode ')) {$options [curlopt_followlocation] = 1;  $options [Curlopt_maxredirs] = 5;  } $headers = $this->get (' headers '); if ($request->options) {$options = $request->options + $Options  } $options [Curlopt_url] = $request->url;   if ($request->post_data && strtolower ($request->method) = = ' Post ') {$options [curlopt_post] = 1;  $options [Curlopt_postfields] = $request->post_data;   } if ($headers) {$options [curlopt_header] = 0;  $options [Curlopt_httpheader] = $headers; } return $options; }/** * Set error message * @access public * @param string $msg */Public Function Set_error ($msg) {if (! empty ($msg)) $th is->errors [] = $msg; /** * Get error message * @access public * @param string $open * @param string $close * @return String */Public function dis  Play_errors ($open = ' <p> ', $close = ' </p> ') {$str = ';  foreach ($this->errors as $val) {$str. = $open. $val. $close; } return $STR; }/** * @access public * @param string $name * @param string $value * @return Boolean */Public function set ($name, $ Value) {if ($name = = ' Options ' | | $name = = ' headers ') {$this->{$name} = $value + $this->{$name};  } else {$this->{$name} = $value; } return TRUE; }/** * * @param string $name * @return Mixed * @access public */Public function get ($name) {return (Isset ($thi s->{$name}))? $this->{$name}: null; }/** * @return void * @access public */Public Function destruct () {unset ($this->size, $this->timeout, $this ->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors); }}//End Curl class/* end of File curl.class.php */

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Implementation code for puppeteer simulation login Crawl page

Vue Data Monitoring Watch usage instructions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.