Multiple ASYNC CURL Requests In PHP – A Simple Example

Welcome to a tutorial on how to do multiple asynchronous CURL requests in PHP. Need to grab data from various websites all at once? Or send data to different servers in parallel?

To run multiple PHP asynchronous CURL requests in parallel:

  1. Initialize the CURL requests and set the options.
    • $chA = curl_init();
    • $chB = curl_init();
    • curl_setopt($chA, CURLOPT_URL, "http://site-A.com");
    • curl_setopt($chB, CURLOPT_URL, "http://site-B.com");
    • curl_setopt($chA, CURLOPT_RETURNTRANSFER, true);
    • curl_setopt($chB, CURLOPT_RETURNTRANSFER, true);
  2. Initialize multi CURL and add the handlers.
    • $mh = curl_multi_init();
    • curl_multi_add_handle($mh, $chA);
    • curl_multi_add_handle($mh, $chB);
  3. Run the multi CURL – do { curl_multi_exec($mh, $active); } while ($active);
  4. Close all the CURL connections when done.
    • curl_multi_remove_handle($mh, $chA);
    • curl_multi_remove_handle($mh, $chB);
    • curl_multi_close($mh);
  5. Fetch the results.
    • $resA = curl_multi_getcontent($chA);
    • $resB = curl_multi_getcontent($chB);

That should cover the basics, but let us walk through another example by “re-package” this snippet – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP Multi CURL Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP MULTI CURL

All right, let us now get into the example of a reusable multi-CURL handler in PHP.

 

MULTI CURL FUNCTION

multi-curl.php
<?php
// (A) MULTI CURL
function mcurl ($list) {
  // (A1) MULTI-CURL INIT
  $mh = curl_multi_init();

  // (A2) CURL INIT
  $multi = []; $i = 0;
  foreach ($list as $url=>$l) {
    $multi[$i] = curl_init();
    curl_setopt($multi[$i], CURLOPT_URL, $url);
    curl_setopt($multi[$i], CURLOPT_RETURNTRANSFER, 1);
    if (isset($l['post'])) {
      curl_setopt($multi[$i], CURLOPT_POST, true);
      curl_setopt($multi[$i], CURLOPT_POSTFIELDS, $l['post']);
    }
    curl_multi_add_handle($mh, $multi[$i]);
    $i++;
  }

  // (A3) CURL EXEC
  do {
    // GET CURL EXEC STATUS
    $status = curl_multi_exec($mh, $active);

    // CREDITS: https://gist.github.com/Xeoncross/2362936
    // WHEN A CURL REQUEST IS COMPLETE - RUN CALLBACK
    if ($state = curl_multi_info_read($mh)) {
      $info = curl_getinfo($state['handle']);
      if (isset($list[$info['url']]['callback'])) {
        $callback = $list[$info['url']]['callback'];
        $callback(curl_multi_getcontent($state['handle']), $info);
      }
      curl_multi_remove_handle($mh, $state['handle']);
    }
    
    // SHORT PAUSE TO NOT FLOOD CPU
    usleep(1000);
  } while ($status == CURLM_CALL_MULTI_PERFORM || $active);
  
  // (A4) CASE CLOSED - ALL DONE
  curl_multi_close($mh);
}

// (B) CALLBACK FUNCTION - JUST OUTPUT RESULTS...
function output ($res) { echo $res; }

// (C) RUN!
mcurl([
  "http://localhost/dummy.php" => [
    "post" => ["KeyA" => "ValueA", "KeyB" => "ValueB"],
    "callback" => "output"
  ],
  "https://en.wikipedia.org/wiki/Portal:Gardening" => [
    "callback" => "output"
  ]
]);

Yep – function mcurl() here is pretty much a “packaged function version” of the above snippet in the introduction.

  • It takes in an array of URL in the format of "URL" => ["post" => DATA-TO-SEND-IF-ANY, "callback" => CALLBACK-FUNCTION]
  • The rest should be pretty straightforward if you follow step-by-step. Create a multi-CURL handler, create individual CURL handlers, set options, run multi-CURL, etc…

That’s all to it. Feel free to adapt and use this in your own projects.

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

THE SUMMARY

Function Description Reference Link
curl_multi_init() Initialize and create a multi CURL handler. Click Here
curl_multi_add_handle() Add a CURL handler to the multi handler. Click Here
curl_multi_exec() Run the multi CURL handler. Click Here
curl_multi_remove_handle() Remove a CURL handler from the multi handler. Click Here
curl_multi_close() Close the multi CURL handler. Click Here
curl_multi_getcontent() Get the result from a CURL handler. Click Here

 

 

INFOGRAPHIC CHEAT SHEET

PHP Multi-CURL Example (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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