Back to Top

Simultaneous cURL requests using curl_multi_exec in PHP

Updated 29 December 2025

Introduction

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

When working with APIs in PHP, performance quickly becomes a concern.
Many applications need to fetch data from external services based on a list of IDs—customers, orders, products, or users.

If these requests are handled one at a time, response time increases linearly and the application feels slow.

This article explains how to make simultaneous cURL requests using curl_multi_exec in PHP, why it matters today, and how to implement it correctly using modern PHP practices.

Why Do Developers Need Multiple cURL Requests?

Start your headless eCommerce
now.
Find out More

Consider a scenario where:

  • An API returns data for one ID per request
  • You need data for 10 or more IDs

If each API call takes 300 ms:

  • 10 requests => 3 seconds
  • 20 requests => 6 seconds

This delay becomes noticeable very quickly, especially in API-driven or headless applications.

How Does a Traditional cURL Approach Work?

The most common implementation uses a loop.
Each request waits for the previous one to finish before starting.

Example: Sequential cURL Requests

/**
* 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);
}

Key limitations of this approach

  • Requests are blocking
  • Network latency adds up
  • Poor performance at scale
  • Not suitable for modern API-heavy systems

What Is curl_multi_exec in PHP and How does it solve the problem?

curl_multi_exec allows PHP to execute multiple HTTP requests at the same time using a single process.
Instead of waiting for one request to complete, PHP:

  • Sends all requests together
  • Monitors them asynchronously
  • Collects responses once they finish

The result is a significant reduction in total execution time.

How Does curl_multi_exec Work Internally?

At a high level:

  • A multi-handle manages multiple cURL handles
  • Each handle represents one HTTP request
  • PHP checks which requests are still running
  • Execution continues until all requests are complete

This approach is efficient, stable, and fully supported in modern PHP versions.

PHP provides native support for parallel HTTP requests using curl_multi_exec, which is documented in the official PHP manual: Link

simultaneous cURL requests using curl_multi_exec in PHP workflow

How to Implement Simultaneous cURL Requests in PHP

/**
* 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);

Why Is curl_multi_exec Faster for Simultaneous cURL Requests in PHP?

  • Requests run in parallel
  • Network wait time overlaps
  • Total execution time is close to the slowest request, not the sum of all requests

This makes it ideal for:

  • API aggregation
  • Microservices communication
  • Headless commerce platforms
  • Background data synchronization

What should developers keep in mind?

  • Always set timeouts to avoid hanging requests
  • Respect API rate limits
  • Batch large ID lists into smaller groups
  • Log errors using curl_error()
  • Close handles properly to avoid memory leaks

Are There Alternatives to curl_multi_exec?

Yes, depending on your application:

  • Framework HTTP clients (Laravel, Symfony)
  • Guzzle with promises
  • Queue-based background jobs
  • Event-driven systems like ReactPHP or Swoole

However, curl_multi_exec is still a solid choice when you need:

  • Low-level control
  • No external dependencies
  • Predictable performance

When Should You Use curl_multi_exec?

Use it when:

  • You need multiple API calls at once
  • Performance is critical
  • You want a lightweight solution

Avoid it when:

  • Requests require complex retry logic
  • You need middleware or caching layers
  • A framework-level client already solves the problem

Frequently Asked Questions

Is curl_multi_exec still supported in PHP?

Yes, the curl_multi_exec function is still supported in the latest versions of PHP.

Does curl_multi_exec block execution?

No. It uses non-blocking execution while managing active requests.

Can authentication headers be used?

Yes. Each cURL handle supports its own headers and auth options.

How many requests should run in parallel?

This depends on server resources and API limits.
In most cases, 5–20 concurrent requests work well.

Conclusion

Making Simultaneous cURL requests using curl_multi_exec in PHP is still a practical and efficient solution for modern applications.

By switching from sequential requests to parallel execution, developers can dramatically improve performance without adding new dependencies.

When used correctly, curl_multi_exec remains a valuable tool for API-driven PHP systems.

. . .

Leave a Comment

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


10 comments

  • Harry
  • HB
    • Kartik Upadhyay (Moderator)
  • 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

    Simultaneous cURL requests using curl_multi_exec in PHP