Using curl_multi_add_handle() to Manage Multiple cURL Handles in PHP
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 demonstrates a complete example that shows initialization, adding handles, executing requests concurrently, retrieving responses, and cleaning up.
The PHP curl library is a widely used tool for making 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.
PHP offers the curl_multi_add_handle() function to add multiple cURL handles to a single multi‑handle session, allowing efficient management of concurrent requests.
The syntax of curl_multi_add_handle() is:
resource curl_multi_add_handle ( resource $mh , resource $ch )Parameters:
$mh – an initialized cURL multi‑handle session.
$ch – the cURL handle to be added to the multi‑handle.
Below is a complete example that demonstrates how to use the function:
<?php
$mh = curl_multi_init(); // initialize 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 results
$response1 = curl_multi_getcontent($ch1);
echo "Response 1: " . $response1 . "\n";
$response2 = curl_multi_getcontent($ch2);
echo "Response 2: " . $response2 . "\n";
// Clean up
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>The script initializes a multi‑handle, creates two independent cURL handles, sets their URLs and options, adds them to the multi‑handle, and then loops with curl_multi_exec() until both requests finish. After execution, curl_multi_getcontent() fetches each response, which is then printed.
Using curl_multi_add_handle() improves network request efficiency and code maintainability by managing multiple handles within a single session. In practice, it can be combined with curl_multi_remove_handle() and curl_multi_close() for more complex request workflows.
In summary, the curl_multi_add_handle() function is a powerful tool for adding several cURL handles to one session, enabling concurrent requests, reducing resource consumption, and simplifying backend HTTP communication in PHP applications.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.