PHP Example Method for Concurrent Requests Using curl_multi

  • 2021-09-20 19:48:56
  • OfStack

This article illustrates how PHP uses curl_multi to implement concurrent requests. Share it for your reference, as follows:


class CurlMultiUtil {
  /**
  *  According to url,postData Get curl Request object , This is relatively simple , You can see the official documents 
  */
  private static function getCurlObject($url,$postData=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($chList){
    $downloader = curl_multi_init();
    //  Will 3 The objects to be requested are put 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;
      }
      // 1 Dan you 1 Request completed, found out, processed , Because curl The bottom layer is select , so it is most limited by 1024
      while ($done = curl_multi_info_read($downloader)){
        //  Get information, content, error from request 
        // $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, block it and put  CPU  Hand over the right to use and avoid the above  do  Infinite loop empty running data leads to  CPU 100%
      if ($running) {
        $rel = curl_multi_select($downloader, 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;
  }
}

For more readers interested in PHP related content, 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", "php Programming Algorithm Summary", "PHP Operation and Operator Usage Summary" and "php Common Database Operation Skills Summary"

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


Related articles: