In PHP, curl_multi is a very powerful tool that is often used to perform concurrent HTTP requests. It allows multiple cURL requests to be made simultaneously without establishing a separate connection for each request, thereby significantly improving the efficiency of network requests. Generally, curl_multi is widely used in scenarios such as requiring processing multiple external API calls and batch acquisition of data. However, how to optimize the performance of these requests is a common challenge for developers when making a large number of requests.
This article will introduce how to improve the performance of curl_multi requests by using the curl_upkeep() function.
curl_multi is a feature provided by the cURL library that allows you to send multiple HTTP requests simultaneously without waiting for each request to complete separately. It can help developers implement asynchronous requests and improve program execution efficiency.
curl_upkeep() is a custom PHP function used to optimize resource management during multi-request processing. It can ensure that during the execution of the request, avoid long-term occupancy of server resources, and can regularly clean and reuse cURL handles to maintain system stability.
First, make sure that the cURL extension is enabled in the PHP environment. Next, we will demonstrate how to implement concurrent requests through curl_multi .
// initialization curl_multi Handle
$multi_handle = curl_multi_init();
// initialization单个 curl Handle
$handles = [];
for ($i = 0; $i < 3; $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://m66.net/api/example" . $i); // Here URL Used m66.net
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 将Handle加入 multi
curl_multi_add_handle($multi_handle, $ch);
$handles[] = $ch;
}
// Execute a request
do {
$multi_status = curl_multi_exec($multi_handle, $active);
} while ($active);
// closure所有Handle
foreach ($handles as $ch) {
curl_multi_remove_handle($multi_handle, $ch);
curl_close($ch);
}
// closure multi Handle
curl_multi_close($multi_handle);
The above code shows how to send three requests simultaneously using curl_multi . Note that the domain name in the URL has been replaced with m66.net , which is what you requested.
In order to improve the performance of curl_multi requests, we need to perform regular cleaning and resource management. The function of the curl_upkeep() function is to maintain the healthy state of the cURL handle during the request process, avoid memory leakage, and ensure the smooth execution of the request.
function curl_upkeep($multi_handle, $handles) {
// Regularly executed,释放掉已经完成的Handle
do {
$status = curl_multi_exec($multi_handle, $active);
// Check if any request has been completed
while ($info = curl_multi_info_read($multi_handle)) {
if ($info['msg'] == CURLMSG_DONE) {
$ch = $info['handle'];
// Get the request result
$response = curl_multi_getcontent($ch);
// Processing response data
echo "Response data: " . $response;
// closure完成的Handle
curl_multi_remove_handle($multi_handle, $ch);
curl_close($ch);
}
}
// Control the frequency of execution of requests,Prevent excessive use CPU
usleep(10000); // 10 millisecond,Avoid excessively high CPU Occupancy
} while ($active); // When there is still a request at runtime,Continue to execute
}
The curl_upkeep() function will periodically check whether the request has been completed and clean up the completed cURL handle in time. During the execution process, the system burden is reduced through appropriate usleep() calls to avoid excessive CPU usage due to frequent polling.
Now, we use the curl_upkeep() function in conjunction with curl_multi to optimize performance.
// initialization curl_multi Handle
$multi_handle = curl_multi_init();
// initialization单个 curl Handle
$handles = [];
for ($i = 0; $i < 5; $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://m66.net/api/example" . $i); // Replace with m66.net domain name
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 将Handle加入 multi
curl_multi_add_handle($multi_handle, $ch);
$handles[] = $ch;
}
// Call upkeep Regular cleaning and resource management of functions
curl_upkeep($multi_handle, $handles);
// closure multi Handle
curl_multi_close($multi_handle);
In this example, the curl_upkeep() function periodically checks the status of each request to ensure that all requests complete smoothly while freeing up the completed resources.
By using curl_upkeep() , the performance of curl_multi requests can be effectively improved, memory leaks can be reduced, system resources can be avoided, and high response speed and stability can be maintained in the case of a large number of concurrent requests. In addition, by regularly cleaning up completed cURL handles, programs can manage requests and system resources more efficiently, thereby improving overall performance.