CURL non blocking call class implemented by PHP

  • 2021-10-27 06:41:02
  • OfStack

This article illustrates the CURL non-blocking calling class implemented by PHP. Share it for your reference, as follows:

The previous article "The Method of Implementing Non-blocking Mode in PHP" describes the implementation of non-blocking mode in PHP. In fact, if it is only HTTP, it can be realized directly with CURL.

Based on a piece of code on the Internet, a non-blocking calling class supporting POST/GET is encapsulated after modification and improvement.

Welcome to test bug ~ ~ ~ ~ ~


/*****************************************************
 CURL  Non-blocking calling class 
 Auther: Linvo
 Copyright(C) 2010/10/21
*******************************************************/
/*
  //  Use example 
  //  Pass in parameter description 
  // url  Request address 
  // data POST Mode data 
  // Concurrent invocation 
  $param1 = array(
      array(
        'url' => "http://localhost/a.php?s=1",
        ),
      array(
        'url' => "http://localhost/a.php?s=1",
        'data' => array('aaa' => 1, 'bbb' => 2),
        ),
      );
  // Single call 
  $param2 = array(
      'url' => "http://localhost/a.php?s=0",
      'data' => array('aaa' => 1, 'bbb' => 2),
      );
  // Single call ( GET Easy way) 
  $param3 = 'http://localhost/a.php?s=2';
  $ac = new AsyncCURL();
  $ac->set_param($param1);
  $ret = $ac->send();
  // The return value is the result array of the request parameter order (the element value is False Indicates a request error) 
  var_dump($ret);
*/
class AsyncCURL
{
  /**
   *  Do you need to return HTTP Header information 
   */
  public $curlopt_header = 0;
  /**
   *  Single CURL Call timeout limit 
   */
  public $curlopt_timeout = 20;
  private $param = array();
  /**
   *  Constructor (you can pass in request parameters directly) 
   *
   * @param array  Optional 
   * @return void
   */
  public function __construct($param = False)
  {
    if ($param !== False)
    {
      $this->param = $this->init_param($param);
    }
  }
  /**
   *  Setting request parameters 
   *
   * @param array
   * @return void
   */
  public function set_param($param)
  {
    $this->param = $this->init_param($param);
  }
  /**
   *  Send a request 
   *
   * @return array
   */
  public function send()
  {
    if(!is_array($this->param) || !count($this->param))
    {
      return False;
    }
    $curl = $ret = array();
    $handle = curl_multi_init();
    foreach ($this->param as $k => $v)
    {
      $param = $this->check_param($v);
      if (!$param) $curl[$k] = False;
      else $curl[$k] = $this->add_handle($handle, $param);
    }
    $this->exec_handle($handle);
    foreach ($this->param as $k => $v)
    {
      if ($curl[$k])
      {
        $ret[$k] = curl_multi_getcontent($curl[$k]);
        curl_multi_remove_handle($handle, $curl[$k]);
      } else {
        $ret[$k] = False;
      }
    }
    curl_multi_close($handle);
    return $ret;
  }
  // The following are private methods 
  private function init_param($param)
  {
    $ret = False;
    if (isset($param['url']))
    {
      $ret = array($param);
    } else {
      $ret = isset($param[0]) ? $param : False;
    }
    return $ret;
  }
  private function check_param($param = array())
  {
    $ret = array();
    if (is_string($param))
    {
      $url = $param;
    } else {
      extract($param);
    }
    if (isset($url))
    {
      $url = trim($url);
      $url = stripos($url, 'http://') === 0 ? $url : NULL;
    }
    if (isset($data) && is_array($data) && !empty($data))
    {
      $method = 'POST';
    } else {
      $method = 'GET';
      unset($data);
    }
    if (isset($url)) $ret['url'] = $url;
    if (isset($method)) $ret['method'] = $method;
    if (isset($data)) $ret['data'] = $data;
    $ret = isset($url) ? $ret : False;
    return $ret;
  }
  private function add_handle($handle, $param)
  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $param['url']);
    curl_setopt($curl, CURLOPT_HEADER, $this->curlopt_header);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, $this->curlopt_timeout);
    if ($param['method'] == 'POST')
    {
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $param['data']);
    }
    curl_multi_add_handle($handle, $curl);
    return $curl;
  }
  private function exec_handle($handle)
  {
    $flag = null;
    do {
      curl_multi_exec($handle, $flag);
    } while ($flag > 0);
  }
}

For more readers interested in PHP related contents, please check the topics of this site: "php curl Usage Summary", "PHP Network Programming Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Data Structure and Algorithm Tutorial" and "json Format Data Operation Skills Summary in PHP"

I hope this article is helpful to everyone's PHP programming.


Related articles: