Using curl_multi_add_handle() in PHP to Manage Multiple cURL Handles
This article explains how PHP's curl_multi_add_handle() function can add multiple cURL handles to a single multi‑handle session, enabling efficient simultaneous network requests, with detailed syntax, parameters, and a complete example demonstrating initialization, execution, response retrieval, and cleanup.
In PHP development, the curl library is commonly used for HTTP requests, and when multiple requests need to be sent simultaneously, creating separate cURL handles can waste resources.
The curl_multi_add_handle() function allows adding multiple cURL handles to a single multi‑handle session for efficient management.
resource curl_multi_add_handle ( resource $mh , resource $ch )The function takes an initialized multi‑handle $mh and a new handle $ch , adds $ch to $mh , and returns a cURL multi handle.
Example code demonstrates initializing a multi‑handle, creating two individual cURL handles, setting options, adding them with curl_multi_add_handle() , executing the requests in a loop with curl_multi_exec() , retrieving each response via curl_multi_getcontent() , and finally cleaning up with curl_multi_remove_handle() and curl_multi_close() .
Using curl_multi_add_handle() improves request efficiency and code maintainability, and can be combined with other multi‑cURL functions for more complex networking tasks.
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.