Backend Development 4 min read

Using PHP curl_multi_add_handle() to Manage Multiple cURL Requests

This article explains how the PHP curl_multi_add_handle() function can add multiple cURL handles to a single multi‑handle session, provides its syntax and parameters, and includes a complete example demonstrating initialization, adding handles, executing requests, retrieving responses, and cleaning up.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP curl_multi_add_handle() to Manage Multiple cURL Requests

The PHP curl library is a widely used tool for performing various HTTP requests such as GET and POST, setting headers, and handling cookies.

When many requests need to be sent simultaneously, creating a separate cURL handle for each can waste resources; the curl_multi_add_handle() function allows multiple handles to be managed within one multi‑handle session.

The syntax of curl_multi_add_handle() is:

resource curl_multi_add_handle ( resource $mh , resource $ch )

The $mh parameter is an already initialized cURL multi‑handle, and $ch is the individual cURL handle to be added to that multi‑handle.

Below is a full example that demonstrates how to use the function:

<?php
$mh = curl_multi_init();  // Initialize a multi‑handle

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://www.example.com/api1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch1);  // Add first handle

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://www.example.com/api2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch2);  // Add second handle

// Execute until all requests are complete
do {
    $status = curl_multi_exec($mh, $running);
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);

// Retrieve and output results
$response1 = curl_multi_getcontent($ch1);
echo "Response 1: " . $response1 . "\n";
$response2 = curl_multi_getcontent($ch2);
echo "Response 2: " . $response2 . "\n";

// Clean up handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>

The script first calls curl_multi_init() to create a multi‑handle, then creates two independent cURL handles ( $ch1 and $ch2 ) with their URLs and options, adds them to the multi‑handle, and runs curl_multi_exec() in a loop until all requests finish.

After execution, curl_multi_getcontent() fetches each response, which is then printed. Finally, the handles are removed and the multi‑handle is closed using curl_multi_remove_handle() and curl_multi_close() .

Using curl_multi_add_handle() together with related functions like curl_multi_remove_handle() and curl_multi_close() enables efficient management of multiple concurrent HTTP requests, improving performance and code maintainability.

In summary, the curl_multi_add_handle() function is a powerful tool for adding several cURL handles to a single session, allowing simultaneous network requests and easier handling of each response.

HTTPPHPNetworkingcurlMulti-Handle
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.