Current Location: Home> Latest Articles> PHP curl_multi_add_handle() Function: Efficiently Manage Multiple cURL Handles in a Session

PHP curl_multi_add_handle() Function: Efficiently Manage Multiple cURL Handles in a Session

M66 2025-06-19

PHP curl_multi_add_handle() Function: Efficiently Manage Multiple cURL Handles in a Session

When developing network requests, PHP's cURL library is one of the most commonly used tools. It provides a variety of functions to perform various HTTP requests, including GET and POST requests, setting request headers, sending and receiving cookies, and more.

During development, there are often situations where we need to send multiple requests at once. If each request uses a separate cURL handle, it may lead to resource wastage. To efficiently manage multiple requests, PHP provides the curl_multi_add_handle() function, which allows multiple cURL handles to be added to a session, simplifying management and improving efficiency.

Syntax of curl_multi_add_handle() Function

The basic syntax of the curl_multi_add_handle() function is as follows:

resource curl_multi_add_handle ( resource $mh, resource $ch )

Explanation of parameters:

  • $mh: The cURL multi handle initialized with curl_multi_init().
  • $ch: The cURL handle to be added to the multi handle.

The function adds the new cURL handle $ch to the multi handle $mh. The return value is a cURL multi handle used for managing multiple requests simultaneously.

Example of Using curl_multi_add_handle()

Below is an example code using the curl_multi_add_handle() function:

<?php
// Initialize a cURL multi handle
$mh = curl_multi_init();
<p>// Create the first cURL handle<br>
$ch1 = curl_init();<br>
curl_setopt($ch1, CURLOPT_URL, "<a rel="noopener" target="_new" class="" href="https://www.example.com/api1">https://www.example.com/api1</a>");<br>
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);</p>
<p>// Add the first cURL handle to the multi handle<br>
curl_multi_add_handle($mh, $ch1);</p>
<p>// Create the second cURL handle<br>
$ch2 = curl_init();<br>
curl_setopt($ch2, CURLOPT_URL, "<a rel="noopener" target="_new" class="" href="https://www.example.com/api2">https://www.example.com/api2</a>");<br>
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);</p>
<p>// Add the second cURL handle to the multi handle<br>
curl_multi_add_handle($mh, $ch2);</p>
<p>// Loop and execute requests until all are completed<br>
do {<br>
$status = curl_multi_exec($mh, $running);<br>
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);</p>
<p>// Get the result of the first request<br>
$response1 = curl_multi_getcontent($ch1);<br>
echo "Response 1: " . $response1;</p>
<p>// Get the result of the second request<br>
$response2 = curl_multi_getcontent($ch2);<br>
echo "Response 2: " . $response2;</p>
<p>// Close the cURL handles<br>
curl_multi_remove_handle($mh, $ch1);<br>
curl_multi_remove_handle($mh, $ch2);<br>
curl_multi_close($mh);<br>
?>

In the above example, we first initialize a cURL multi handle $mh using the curl_multi_init() function. Then, we create two separate cURL handles $ch1 and $ch2 and set their request parameters. After that, we add these two handles to the multi handle using the curl_multi_add_handle() function. With the curl_multi_exec() function, we execute multiple requests in a loop until all requests are completed. Finally, we use the curl_multi_getcontent() function to fetch and output the results of each request.

Conclusion

The curl_multi_add_handle() function is an essential tool in PHP for managing multiple cURL handles simultaneously, improving the efficiency of network requests and optimizing the code structure. In real-world development, we can also combine it with functions like curl_multi_remove_handle() and curl_multi_close() to further refine the request handling process.

By using the curl_multi_add_handle() function, we can efficiently manage and execute multiple HTTP requests in PHP, boosting performance and code maintainability.