When using curl_multi_exec() for concurrent requests, developers will often notice that the function can become complicated during the request process, especially when multiple requests are involved. At this time, curl_upkeep() becomes particularly important. This article will explain why curl_upkeep () needs to be called after using curl_multi_exec() and how to implement it correctly.
In PHP, the curl_multi_exec() function is a very useful tool that allows us to handle multiple cURL sessions simultaneously. This is very useful for applications that require multiple HTTP requests to be executed concurrently, such as crawling multiple web page data and processing API requests, which can greatly improve efficiency.
However, curl_multi_exec() itself does not handle some underlying work, such as regular cleaning and maintenance of session resources. At this time, curl_upkeep() plays a crucial role, helping us maintain and manage the healthy state of cURL sessions.
When curl_multi_exec() is used, it starts multiple cURL requests concurrently and continuously checks the status of each request. When all requests are executed, curl_multi_exec() will return a flag indicating whether the operation is completed.
However, curl_multi_exec() does not automatically clean up internal resources. If curl_upkeep() is not called regularly, it may cause resource leakage or the session cannot be closed correctly. The main function of curl_upkeep() is to process and clean these session resources regularly to avoid performance issues or memory leaks.
In practical applications, we need to execute curl_upkeep() regularly during curl_multi_exec() call to ensure that the session resources are managed properly. Here is a common example of using curl_multi_exec() and curl_upkeep() :
<?php
// initialization cURL resource
$mh = curl_multi_init();
// Define an array to store multiple cURL Session
$ch1 = curl_init('http://m66.net/api/endpoint1');
$ch2 = curl_init('http://m66.net/api/endpoint2');
$ch3 = curl_init('http://m66.net/api/endpoint3');
// Set each cURL Session的选项
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
// Will cURL Session添加到 multi-handle
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
curl_multi_add_handle($mh, $ch3);
// Perform concurrent requests
do {
$status = curl_multi_exec($mh, $active);
// Perform regularly curl_upkeep() 来确保resource被清理和管理
curl_upkeep($mh);
} while ($active);
// Get the response result for each request
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
$response3 = curl_multi_getcontent($ch3);
// closure cURL Session
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_remove_handle($mh, $ch3);
// closure multi-handle resource
curl_multi_close($mh);
?>
In the example code above, we initialize three cURL sessions and add them to curl_multi_exec() to execute. In each loop, we call curl_upkeep() to ensure the cleanup and maintenance of resources.
The functions of curl_upkeep() include but are not limited to:
Check the status of each cURL session to make sure their requests are completed.
Regularly clean up timeouts or no longer needed cURL sessions to avoid unnecessary memory or system resources.
Perform appropriate error handling and status updates during execution.
Although PHP's cURL extension does not have an explicit curl_upkeep() function, it only serves as a concept to remind developers to regularly manage and clean resources when executing concurrent requests, ensuring the robustness and efficiency of the program.
curl_multi_exec() is a very powerful concurrent request tool in PHP, but when concurrently executing requests, it is very important to call curl_upkeep() (or a similar resource management mechanism). This ensures that the system does not crash due to resource leaks or session management issues, thereby improving program stability and performance. Ensure that cURL sessions are regularly cleaned and managed when performing concurrent requests to ensure that the program runs efficiently.