PHP Curl Batch for controllable concurrent asynchronous operations

Source: Internet
Author: User
This article mainly introduces the PHP Curl batch processing to achieve controllable concurrency asynchronous operation, has a certain reference value, now share to everyone, the need for friends can refer to

This article describes the PHP Curl batch process to achieve controllable concurrent asynchronous operations. Share to everyone for your reference, as follows:

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, the number of concurrent control, the application of a lot, let yourself play the imagination.

<?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, $OP  tions = 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, $t His->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-&GT;CA Llback = $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 */P ublic function Request ($url, $method = ' GET ', $post _data = null, $headers = NULL, $options = null) {$this->requests [  ] = new 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 ($t his->{$name}))? $this->{$name}: null; }/** * @return void * @access public */Public function __destruct () {unset ($this->size, $this->timeout, $th Is->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors) ; }}//End Curl class/* end of File curl.class.php */

The above is the entire content of this article, more relevant content please pay attention to topic.alibabacloud.com.

Related Article

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.