Home Backend Development PHP Tutorial PHP curl batch processing realizes controllable concurrent and asynchronous operations

PHP curl batch processing realizes controllable concurrent and asynchronous operations

May 09, 2018 am 10:26 AM
curl php Batch processing

This article mainly introduces the controllable concurrency and asynchronous operation of php curl batch processing. It has certain reference value. Now I share it with you. Friends in need can refer to it.

The examples in this article are described PHP curl batch processing implements controllable concurrent asynchronous operations. Share it with everyone for your reference, the details are as follows:

Usually cURL in PHP runs in a blocking manner, which means that after creating a cURL request, you must wait for it to execute successfully or time out before executing the next request: API CURL is generally preferred for interface access. In actual projects or in the process of writing your own gadgets (such as news aggregation, commodity price monitoring, price comparison), you usually need to obtain data from a third-party website or API interface. When processing a URL queue, in order to improve performance, you can use the

curl_multi_*

family of functions provided by cURL to achieve simple concurrency. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;?php include &amp;#39;curl.class.php&amp;#39;; function callback($response, $info, $error, $request) { echo &amp;#39;response:&lt;br&gt;&amp;#39;; print_r($response); echo &amp;#39;&lt;br&gt;&amp;#39; . date(&quot;Y-m-d H:i:s&quot;) . &amp;#39; &lt;br&gt;&amp;#39;; echo &amp;#39;&lt;br&gt;&amp;#39; . str_repeat(&quot;-&quot;, 100) . &amp;#39;&lt;br&gt;&amp;#39;; } $USER_COOKIE = (!empty($_REQUEST[&amp;#39;cookie&amp;#39;])) ? $_REQUEST[&amp;#39;cookie&amp;#39;] : file_get_contents(&quot;cookie.txt&quot;); $curl = new Curl (&quot;callback&quot;); $data = array( array( &amp;#39;url&amp;#39; =&gt; &amp;#39;http://dyactive2.vip.xunlei.com/com_sign/?game=qmr&amp;type=rec_gametime&amp;referfrom=&amp;rt=0.42521539455332336&amp;#39;, //秦美人 &amp;#39;method&amp;#39; =&gt; &amp;#39;POST&amp;#39;, &amp;#39;post_data&amp;#39; =&gt; &amp;#39;&amp;#39;, &amp;#39;header&amp;#39; =&gt; null, &amp;#39;options&amp;#39; =&gt; array( CURLOPT_REFERER =&gt; &quot;http://niu.xunlei.com/entergame/?gameNo=qmr&amp;fenQuNum=3&quot;, CURLOPT_COOKIE =&gt; $USER_COOKIE, ) ), array( &amp;#39;url&amp;#39; =&gt; &amp;#39;http://dyactive2.vip.xunlei.com/com_sign/?game=sq&amp;type=rec_gametime&amp;referfrom=&amp;rt=0.42521539455332336&amp;#39;, //神曲 &amp;#39;method&amp;#39; =&gt; &amp;#39;POST&amp;#39;, &amp;#39;post_data&amp;#39; =&gt; &amp;#39;&amp;#39;, &amp;#39;header&amp;#39; =&gt; null, &amp;#39;options&amp;#39; =&gt; array( CURLOPT_REFERER =&gt; &quot;http://niu.xunlei.com/entergame/?gameNo=sq&amp;fenQuNum=41&quot;, CURLOPT_COOKIE =&gt; $USER_COOKIE, ) ), array( &amp;#39;url&amp;#39; =&gt; &amp;#39;http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&amp;type=rec_gametime&amp;referfrom=&amp;rt=0.42521539455332336&amp;#39;, //凡人修真 &amp;#39;method&amp;#39; =&gt; &amp;#39;POST&amp;#39;, &amp;#39;post_data&amp;#39; =&gt; &amp;#39;&amp;#39;, &amp;#39;header&amp;#39; =&gt; null, &amp;#39;options&amp;#39; =&gt; array( CURLOPT_REFERER =&gt; &quot;http://niu.xunlei.com/entergame/?gameNo=frxz&amp;fenQuNum=3&quot;, CURLOPT_COOKIE =&gt; $USER_COOKIE, ) ), array( &amp;#39;url&amp;#39; =&gt; &amp;#39;http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&amp;type=rec_gametime&amp;referfrom=&amp;rt=0.42521539455332336&amp;#39;, //神魔仙界 &amp;#39;method&amp;#39; =&gt; &amp;#39;POST&amp;#39;, &amp;#39;post_data&amp;#39; =&gt; &amp;#39;&amp;#39;, &amp;#39;header&amp;#39; =&gt; null, &amp;#39;options&amp;#39; =&gt; array( CURLOPT_REFERER =&gt; &quot;http://niu.xunlei.com/entergame/?gameNo=smxj&amp;fenQuNum=2&quot;, CURLOPT_COOKIE =&gt; $USER_COOKIE, ) ), array( &amp;#39;url&amp;#39; =&gt; &amp;#39;http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&amp;type=rec_gametime&amp;referfrom=&amp;rt=0.42521539455332336&amp;#39;, //倾世情缘 &amp;#39;method&amp;#39; =&gt; &amp;#39;POST&amp;#39;, &amp;#39;post_data&amp;#39; =&gt; &amp;#39;&amp;#39;, &amp;#39;header&amp;#39; =&gt; null, &amp;#39;options&amp;#39; =&gt; array( CURLOPT_REFERER =&gt; &quot;http://niu.xunlei.com/entergame/?gameNo=qsqy&amp;fenQuNum=11&quot;, CURLOPT_COOKIE =&gt; $USER_COOKIE, ) ), ); foreach ($data as $val) { $request = new Curl_request ($val [&amp;#39;url&amp;#39;], $val [&amp;#39;method&amp;#39;], $val [&amp;#39;post_data&amp;#39;], $val [&amp;#39;header&amp;#39;], $val [&amp;#39;options&amp;#39;]); $curl-&gt;add($request); } $curl-&gt;execute(); echo $curl-&gt;display_errors();</pre><div class="contentsignin">Copy after login</div></div> The effect is very good, there are no side effects, and the number of concurrency can be Control, there are many applications, use your imagination

<?php
/**
 * cURL批量处理 工具类
 * 
 * @since Version 1.0
 * @author Justmepzy <justmepzy@gmail.com>
 * @link http://t.qq.com/JustPzy
 */
/**
 *单一的请求对象
 */
class Curl_request {
 public $url   = &#39;&#39;;
 public $method   = &#39;GET&#39;;
 public $post_data  = null;
 public $headers  = null;
 public $options  = null;
 /**
  * 
  * @param string $url
  * @param string $method
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return void
  */
 public function __construct($url, $method = &#39;GET&#39;, $post_data = null, $headers = null, $options = 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, $this->headers, $this->options );
 }
}
/**
 * 包含请求列队处理
 */
class Curl {
 /**
  * 请求url个数
  * @var int
  */
 private $size    = 5;
 /**
  * 等待所有cURL批处理中的活动连接等待响应时间
  * @var int
  */
 private $timeout   = 5;
 /**
  * 完成请求回调函数
  * @var string
  */
 private $callback   = null;
 /**
  * cRUL配置
  * @var array
  */
 private $options   = array (CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_CONNECTTIMEOUT => 30 );
 /**
  * 请求头
  * @var array
  */
 private $headers   = array ();
 /**
  * 请求列队
  * @var array
  */
 private $requests   = array ();
 /**
  * 请求列队索引
  * @var array
  */
 private $request_map  = array ();
 /**
  * 错误
  * @var array
  */
 private $errors   = array ();
 /**
  * @access public
  * @param string $callback 回调函数
  * 该函数有4个参数($response,$info,$error,$request)
  * $response url返回的body
  * $info  cURL连接资源句柄的信息
  * $error  错误
  * $request  请求对象
  */
 public function __construct($callback = null) {
  $this->callback = $callback;
 }
 /**
  * 添加一个请求对象到列队
  * @access public
  * @param object $request
  * @return boolean
  */
 public function add($request) {
  $this->requests [] = $request;
  return TRUE;
 }
 /**
  * 创建一个请求对象并添加到列队
  * @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 = &#39;GET&#39;, $post_data = null, $headers = null, $options = null) {
  $this->requests [] = new Curl_request ( $url, $method, $post_data, $headers, $options );
  return TRUE;
 }
 /**
  * 创建GET请求对象
  * @access public
  * @param string $url
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function get($url, $headers = null, $options = null) {
  return $this->request ( $url, "GET", null, $headers, $options );
 }
 /**
  * 创建一个POST请求对象
  * @access public
  * @param string $url
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function post($url, $post_data = null, $headers = null, $options = null) {
  return $this->request ( $url, "POST", $post_data, $headers, $options );
 }
 /**
  * 执行cURL
  * @access public
  * @param int $size 最大连接数
  * @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 );
  }
 }
 /**
  * 单个url请求
  * @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&#39;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;
 }
 /**
  * 多个url请求
  * @access private
  * @param int $size 最大连接数
  * @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 ( &#39;size must be greater than 1&#39; );
  $master = curl_multi_init ();
  //添加cURL连接资源句柄到map索引
  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;
   //有一个请求完成则回调
   while ( $done = curl_multi_info_read ( $master ) ) {
    //$done 完成的请求句柄
    $info = curl_getinfo ( $done [&#39;handle&#39;] );//
    $output = curl_multi_getcontent ( $done [&#39;handle&#39;] );//
    $error = curl_error ( $done [&#39;handle&#39;] );//
    $this->set_error ( $error );
    //调用回调函数,如果存在的话
    $callback = $this->callback;
    if (is_callable ( $callback )) {
     $key = ( string ) $done [&#39;handle&#39;];
     $request = $this->requests [$this->request_map [$key]];
     unset ( $this->request_map [$key] );
     call_user_func ( $callback, $output, $info, $error, $request );
    }
    curl_close ( $done [&#39;handle&#39;] );
    //从列队中移除已经完成的request
    curl_multi_remove_handle ( $master, $done [&#39;handle&#39;] );
   }
   //等待所有cURL批处理中的活动连接
   if ($active)
    curl_multi_select ( $master, $this->timeout );
  } while ( $active );
  //完成关闭
  curl_multi_close ( $master );
  return true;
 }
 /**
  * 获取没得请求对象的cURL配置
  * @access private
  * @param object $request
  * @return array
  */
 private function get_options($request) {
  $options = $this->__get ( &#39;options&#39; );
  if (ini_get ( &#39;safe_mode&#39; ) == &#39;Off&#39; || ! ini_get ( &#39;safe_mode&#39; )) {
   $options [CURLOPT_FOLLOWLOCATION] = 1;
   $options [CURLOPT_MAXREDIRS] = 5;
  }
  $headers = $this->__get ( &#39;headers&#39; );
  if ($request->options) {
   $options = $request->options + $options;
  }
  $options [CURLOPT_URL] = $request->url;
  if ($request->post_data && strtolower($request->method) == &#39;post&#39; ) {
   $options [CURLOPT_POST] = 1;
   $options [CURLOPT_POSTFIELDS] = $request->post_data;
  }
  if ($headers) {
   $options [CURLOPT_HEADER] = 0;
   $options [CURLOPT_HTTPHEADER] = $headers;
  }
  return $options;
 }
 /**
  * 设置错误信息
  * @access public
  * @param string $msg
  */
 public function set_error($msg) {
  if (! empty ( $msg ))
   $this->errors [] = $msg;
 }
 /**
  * 获取错误信息
  * @access public
  * @param string $open
  * @param string $close
  * @return string
  */
 public function display_errors($open = &#39;<p>&#39;, $close = &#39;</p>&#39;) {
  $str = &#39;&#39;;
  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 == &#39;options&#39; || $name == &#39;headers&#39;) {
   $this->{$name} = $value + $this->{$name};
  } else {
   $this->{$name} = $value;
  }
  return TRUE;
 }
 /**
  * 
  * @param string $name
  * @return mixed
  * @access public
  */
 public function __get($name) {
  return (isset ( $this->{$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 */
Copy after login

The above is the entire content of this article, please pay attention to the PHP Chinese website for more related content.

Related recommendations:

php curl sends a fake request

php CURL obtains cookies to simulate login method code example

The above is the detailed content of PHP curl batch processing realizes controllable concurrent and asynchronous operations. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles