Using PHP curl_multi_add_handle() to Manage Multiple cURL Handles
This article explains how the PHP curl_multi_add_handle() function can add multiple cURL handles to a single session, providing syntax, parameter details, and a complete example that demonstrates initializing, adding, executing, retrieving results, and cleaning up parallel HTTP requests efficiently.
In network request development, the PHP curl library is a widely used tool that offers many functions for performing various HTTP requests, such as GET and POST, setting headers, and handling cookies.
When sending multiple requests, creating a separate cURL handle for each request can waste resources; the curl_multi_add_handle() function allows you to add several cURL handles to one session for better management.
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 new cURL handle to be added to the multi‑handle.
Below is a complete example that demonstrates the usage of curl_multi_add_handle() :
<?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 finished
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() , then creates two independent cURL handles ( $ch1 and $ch2 ) and sets their URLs and options. Each handle is added to the multi‑handle using curl_multi_add_handle() . A loop runs curl_multi_exec() until all requests finish, after which curl_multi_getcontent() fetches each response and outputs it. Finally, the handles are removed and the multi‑handle is closed.
Using curl_multi_add_handle() efficiently manages multiple cURL sessions, improving network request performance and code maintainability. In practice, you can combine it with curl_multi_remove_handle() and curl_multi_close() for more complex request workflows.
In summary, curl_multi_add_handle() is a valuable function that lets you add several cURL handles to a single session, enabling concurrent requests, higher efficiency, and easier maintenance 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.