Send multiple asynchronous cURL requests using PHP

Anil Chaudhari
2 min readJun 28, 2023

--

In the digital world, web apps often rely on external APIs for data retrieval and other tasks. cURL is a potent tool that facilitates HTTP requests in PHP. By utilizing cURL’s asynchronous capabilities, we can enhance our application’s efficiency and responsiveness by concurrently executing multiple requests.

To improve efficiency, we can use asynchronous requests in PHP. Instead of sending HTTP requests sequentially and waiting for each response, asynchronous requests allow simultaneous execution, reducing waiting times caused by network delays or time-consuming requests. This approach significantly improves the speed of processing multiple requests.

Asynchronous requests handle multiple requests simultaneously, reducing execution time. cURL in PHP supports asynchronous requests by initiating multiple handles concurrently, maximizing the benefits of this approach.

To utilize cURL for asynchronous requests, we follow these steps:

  1. Initialize a multi-handle: We create a cURL multi-handle using the curl_multi_init() function. This handle serves as a container for multiple individual cURL handles.
  2. Add individual cURL handles: We create individual cURL handles using the curl_init() function for each request we want to make. We can set the request URL, headers, data, and other options specific to each request using curl_setopt().
  3. Add handles to the multi-handle: We add the individual cURL handles to the multi-handle using curl_multi_add_handle(). This step tells the multi-handle about the requests it needs to process concurrently.
  4. Execute the requests: We use curl_multi_exec() to initiate the execution of the requests. This function starts the requests in parallel.
  5. Step 5: Handling Responses: To retrieve the responses of the asynchronous requests, you need to iterate over the handles and check their status. You can extract the response data using curl_multi_getcontent().
  6. Cleaning Up: Finally, it’s essential to clean up the resources allocated for the cURL handles and multi-handle. You should remove each handle from the multi-handle using curl_multi_remove_handle() and then close the individual cURL handles using curl_close(). Don't forget to close the multi-handle using curl_multi_close().

Demo Code

$urls = [
'https://api.genderize.io?name=anil',
.
.
];

$mh = curl_multi_init();

$requests = [];
foreach ($urls as $i => $url) {
$requests[$i] = curl_init($url);
curl_setopt($requests[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($requests[$i], CURLOPT_TIMEOUT, 10);
curl_setopt($requests[$i], CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($requests[$i], CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($requests[$i], CURLOPT_SSL_VERIFYPEER, false);
curl_multi_add_handle($mh, $requests[$i]);
}

$active = null;
do {
curl_multi_exec($mh, $active);
} while ($active);


$response = [];
foreach ($requests as $request) {
$response[] = curl_multi_getcontent($request);
curl_multi_remove_handle($mh, $request);
curl_close($request);
}

curl_multi_close($mh);

print_r($response);

Source code for comparing async vs sync.

Conclusion: Making multiple asynchronous cURL requests using PHP enables you to enhance the performance and responsiveness of your applications. By utilizing the cURL library and the curl_multi_* functions, you can execute multiple requests concurrently, significantly reducing the overall processing time. Whether you need to fetch data from multiple APIs, scrape web pages, or perform any other task involving multiple HTTP requests, sending asynchronous cURL requests in PHP will undoubtedly boost your application's efficiency.

--

--