Reading list Switch to dark mode

    Simultaneous cURL requests using curl_multi_exec in PHP

    Updated 16 July 2021

    Introduction

    Why we are discussing this(multiple cURL requests/curl_multi_exec).

    Let’s take an example where we need to fetch records from the server based on ids assuming that server will return data for single id in a single request.

    Now, if we use cURL then we need to iterate loop for each id and request server therefore if there are 10 ids then on an average response time will be 10X of a single request.

    /**
    * Webkul Software.
    *
    * @category Webkul
    * @author Webkul
    * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
    * @license https://store.webkul.com/license.html
    */
    
    $result=array();
    foreach ($ids as $id) {
      // URL from which data will be fetched
      $fetchURL = 'https://webkul.com&customerId='.$id;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $fetchURL);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $result[] = curl_exec($ch);
      curl_close($ch);
    }
    

    Solution

    But if use multiple cURL requests then response time reduces significantly.

    /**
    * Webkul Software.
    *
    * @category Webkul
    * @author Webkul
    * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
    * @license https://store.webkul.com/license.html
    */
    
    // array of curl handles
    $multiCurl = array();
    // data to be returned
    $result = array();
    // multi handle
    $mh = curl_multi_init();
    foreach ($ids as $i => $id) {
      // URL from which data will be fetched
      $fetchURL = 'https://webkul.com&customerId='.$id;
      $multiCurl[$i] = curl_init();
      curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL);
      curl_setopt($multiCurl[$i], CURLOPT_HEADER,0);
      curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1);
      curl_multi_add_handle($mh, $multiCurl[$i]);
    }
    $index=null;
    do {
      curl_multi_exec($mh,$index);
    } while($index > 0);
    // get content and remove handles
    foreach($multiCurl as $k => $ch) {
      $result[$k] = curl_multi_getcontent($ch);
      curl_multi_remove_handle($mh, $ch);
    }
    // close
    curl_multi_close($mh);
    

    Result for each cURL request is in $result array some what like this:

    Start your headless eCommerce
    now.
    Find out More
    $result[0] => {"id1":{"customerName":"John","customerAddress":...
    $result[1] => {"id2":{"customerName":"JDoe","customerAddress":...
    ....
    

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    8 comments

  • Luis Javier Álvarez Rodríguez
    • Arjun Singh (Moderator)
  • Rifat
  • kleber
    • Arjun Singh (Moderator)
  • kleber
  • Difster
    • Arjun Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content