Menu Close
    Searching for an experienced eCommerce & Marketplace Development Company ?

    Simultaneous cURL requests using curl_multi_exec in PHP

    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:

    $result[0] => {"id1":{"customerName":"John","customerAddress":...
    $result[1] => {"id2":{"customerName":"JDoe","customerAddress":...
    ....
    
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    8 comments

  • Luis Javier Álvarez Rodríguez
    Hello! I’m trying to adequate your tutorial to a PHP script I’m writing but I’m not getting it right. Can you take a look and let me know if you have any recommendations?
    This is my GitHub: https://github.com/ljalvrdz/mex_sat

    I would apreciate it I can’t seem to find the answer to make this new multiCurl working, and making one request at the time takes to long when many records to validate.

    Thanks!

    • Arjun Singh (Moderator)
      Hello,

      There are several issues in your code(satitv3.php).

      1. Around line 31,$multiCurl[$data] = curl_init(); $data is array and can not be used as index. You may initialize a variable for example $i=0; before your while loop and increment it in your while loop.

      2. You are executing whole stuff in the while loop where as you only need to set the data in the while loop and execute it after while loop. As in above example we, only set data in foreach loop and execute the curl after forach loop.
      In your case, you did not get any benefit of curl_multi_exec as you are execute it each time your while loop iterates.

  • Rifat
    This tutorial worked really like a charm! Thanks, Mr. Arjun Singh. 🙂
  • kleber
    • Arjun Singh (Moderator)
      The example used here is just for reference you need to use actual data to get the result.
      $ids used here is just for reference. You need to pass ids array for which you need to fetch data from server.
      The error mentioned in the screen-shot is due to no variable with name $ids declared before using in foreach.
  • kleber
  • Difster
    Excellent information but line 20 should be:
    curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL);

    Notice the change from $fetch_url to $fetchURL

    • Arjun Singh (Moderator)
      Thank you Difster for pointing out the mistake. It is updated Now.
  • Back to Top

    Table of Content

    Hide Index