Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Simultaneous cURL requests using curl_multi_exec in PHP

    Introduction

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

    While using curl in our PHP application to make web requests, we probably realized that by doing them one after the other, the total time of the request is the sum of all the requests put together.

    Let’s say that PHP application needs to hit different servers or same server with different data. We need to make curl request for each of the server. Which add to the total response time.

    By using curl_multi_exec, We can execute all these requests in parallel, and we will only be limited by the slowest request.

    This way our app is as slow as the slowest request only.

    Example

    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.

    Solution

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


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

    . . .

    Comments (8)

    Add Your Comment

  • 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
      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
      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
      Thank you Difster for pointing out the mistake. It is updated Now.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close

    Table of Content

    Hide Index