Using curl_multi_add_handle() in PHP to Manage Multiple cURL Requests
This article explains how the PHP curl_multi_add_handle() function can be used to add multiple cURL handles to a single session, provides its syntax and parameters, and includes a complete example demonstrating initialization, adding handles, executing requests, retrieving responses, and cleaning up.
In PHP network request development, the curl library is a commonly used tool that offers many functions for performing various HTTP requests such as GET and POST, setting request headers, and handling cookies.
When sending multiple requests simultaneously, creating a separate cURL handle for each request can waste resources. PHP provides the curl_multi_add_handle() function to add multiple cURL handles to a single session for efficient management.
The syntax of curl_multi_add_handle() is:
resource curl_multi_add_handle ( resource $mh , resource $ch )Parameters:
$mh : an initialized cURL multi session handle.
$ch : the new cURL handle to be added to the multi session.
The function adds the $ch handle to the $mh multi handle and returns a cURL multi handle.
Below is a sample code using curl_multi_add_handle() :
<?php
$mh = curl_multi_init(); // Initialize a cURL multi session
$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 example first initializes a multi handle with curl_multi_init() , creates two independent cURL handles ( $ch1 and $ch2 ), sets their URLs and options, and adds them to the multi session using curl_multi_add_handle() . It then loops with curl_multi_exec() until all requests finish, retrieves each response with curl_multi_getcontent() , and finally removes the handles and closes the session.
Using curl_multi_add_handle() efficiently manages multiple cURL handles, improving network request performance and code maintainability. In practice, it can be combined with other functions such as curl_multi_remove_handle() and curl_multi_close() for more complex request workflows.
In summary, the curl_multi_add_handle() function is a valuable tool for adding multiple cURL handles to a single session, thereby enhancing request efficiency and code maintainability in real‑world development.
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.