Current Location: Home> Latest Articles> How to Use curl_upkeep? Learn How to Properly Call curl_upkeep After curl_multi_add_handle()

How to Use curl_upkeep? Learn How to Properly Call curl_upkeep After curl_multi_add_handle()

M66 2025-06-23

When using the cURL library in PHP for multi-threaded requests, the curl_multi_add_handle() function is a critical step. It allows you to add multiple cURL handles to a cURL multi-handle. However, during multi-threaded requests, you may encounter situations where you need to properly manage and maintain the multi-handle. In such cases, the curl_upkeep() function is very useful. It ensures that multiple concurrent requests remain active after calling curl_multi_add_handle() and cleans up invalid requests in a timely manner.

This article will explain in detail how to properly call the curl_upkeep() function after using curl_multi_add_handle() and ensure that cURL multi-threaded requests are executed smoothly.

1. What is the curl_multi_add_handle() Function?

curl_multi_add_handle() is a PHP function used to add multiple cURL handles to a multi-handle. It allows you to make multiple HTTP requests concurrently without creating a new cURL session for each request. After calling this function, you can use curl_multi_exec() to execute and wait for all requests to finish.

For example, here’s a simple example of a multi-threaded cURL request:

$mh = curl_multi_init(); // Initialize a multi-handle
<p>// Create multiple cURL handles<br>
$ch1 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data1">http://m66.net/api/data1</a>');<br>
$ch2 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data2">http://m66.net/api/data2</a>');<br>
$ch3 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data3">http://m66.net/api/data3</a>');</p>
<p>// Add handles to the multi-handle<br>
curl_multi_add_handle($mh, $ch1);<br>
curl_multi_add_handle($mh, $ch2);<br>
curl_multi_add_handle($mh, $ch3);<br>

2. The Role of curl_upkeep()

curl_upkeep() ensures that all requests in the multi-handle remain active and helps manage the requests that are no longer needed. It helps clean up completed or failed requests, allowing the system to reclaim resources in a timely manner, thus avoiding unnecessary memory consumption.

3. How to Call curl_upkeep() After curl_multi_add_handle()

After adding multiple cURL handles to the multi-handle, you need to periodically call curl_upkeep() to manage the lifecycle of these handles. The specific steps are as follows:

  1. Call curl_multi_exec() to initiate the requests.

  2. Use curl_upkeep() to maintain the active state of the cURL sessions.

  3. After all requests are completed, remove the finished handles.

Here’s an example:

$mh = curl_multi_init(); // Initialize multi-handle
<p>// Create multiple cURL handles<br>
$ch1 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data1">http://m66.net/api/data1</a>');<br>
$ch2 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data2">http://m66.net/api/data2</a>');<br>
$ch3 = curl_init('<a rel="noopener" target="_new" class="" href="http://m66.net/api/data3">http://m66.net/api/data3</a>');</p>
<p>// Add handles to the multi-handle<br>
curl_multi_add_handle($mh, $ch1);<br>
curl_multi_add_handle($mh, $ch2);<br>
curl_multi_add_handle($mh, $ch3);</p>
<p>// Execute the requests<br>
$running = null;<br>
do {<br>
curl_multi_exec($mh, $running);  // Execute requests<br>
curl_upkeep($mh);  // Maintain the multi-handle<br>
} while ($running);</p>
<p>// Get the results of each request<br>
$response1 = curl_multi_getcontent($ch1);<br>
$response2 = curl_multi_getcontent($ch2);<br>
$response3 = curl_multi_getcontent($ch3);</p>
<p>// Close the handles<br>
curl_multi_remove_handle($mh, $ch1);<br>
curl_multi_remove_handle($mh, $ch2);<br>
curl_multi_remove_handle($mh, $ch3);</p>
<p>// Close the multi-handle<br>
curl_multi_close($mh);<br>

4. Why Use curl_upkeep()?

When using curl_multi_exec() to execute concurrent requests, we cannot guarantee that all requests will finish on the first execution. Sometimes, certain requests may take longer. curl_upkeep() can periodically clean up completed request handles to prevent them from consuming unnecessary resources. Additionally, curl_upkeep() ensures that even in busy request scenarios, the program can continue running efficiently.

5. Conclusion

When handling multiple concurrent requests in PHP with curl_multi_add_handle(), curl_upkeep() is an essential tool. It helps us manage the multi-handle and clean up invalid requests in a timely manner, ensuring efficient request execution and proper resource usage. By properly using curl_upkeep(), you can make concurrent requests in PHP more stable and reliable.