Current Location: Home> Latest Articles> How to clean the handle of a duplicate request through curl_multi_close

How to clean the handle of a duplicate request through curl_multi_close

gitbox 2025-05-12

How to effectively clean the handles of repeated requests through the curl_multi_close function to avoid resource waste and improve performance?

In PHP, cURL is a powerful library for exchanging data with other servers via URL. Especially when you need to handle multiple concurrent requests, using the curl_multi_* series of functions will make things more efficient. However, if resources are not properly cleaned, especially handles to duplicate requests, it can lead to waste of resources and impact performance.

This article will introduce how to effectively clean the handles of duplicate requests through the curl_multi_close function to avoid resource waste and improve the performance of PHP scripts.

1. What is the curl_multi_close function?

curl_multi_close is a function in the PHP cURL library that closes a handle to a multiple cURL request. It is mainly used to close multiple cURL request handles created when using curl_multi_exec and curl_multi_add_handle functions. Calling this function can effectively free up system resources occupied by multiple requests, avoiding resource leakage or excessive memory usage.

2. Why use curl_multi_close ?

When using multiple cURL requests, multiple requests share the same multiple cURL handle, which can improve execution efficiency. However, if each request is not closed correctly after it is finished, these handles will continue to occupy system resources, which may eventually lead to performance issues or memory leaks.

curl_multi_close ensures:

  • Release resources after each request is completed

  • System resources are effectively utilized

  • Improve program response speed and performance

3. Basic cURL multiple request operation

Here is a simple example that demonstrates how to use cURL multiple requests to process multiple requests in parallel.

 <?php

// initializationcURLMultiple handles
$multiHandle = curl_multi_init();

// askURLsArray
$urls = [
    'https://gitbox.net/api/data1',
    'https://gitbox.net/api/data2',
    'https://gitbox.net/api/data3',
];

// storagecURLHandle
$curlHandles = [];

// Create eachcURLask
foreach ($urls as $i => $url) {
    $curlHandles[$i] = curl_init($url);
    curl_setopt($curlHandles[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiHandle, $curlHandles[$i]);
}

// 执行多重ask
$running = null;
do {
    curl_multi_exec($multiHandle, $running);
} while ($running);

// 获取每个ask的结果
foreach ($curlHandles as $i => $ch) {
    $response = curl_multi_getcontent($ch);
    echo "Response from request $i: $response\n";
}

// Close eachcURLHandle
foreach ($curlHandles as $ch) {
    curl_multi_remove_handle($multiHandle, $ch);
    curl_close($ch);
}

// 清理Multiple handles
curl_multi_close($multiHandle);

?>

4. How to avoid resource waste caused by repeated request handles?

When executing multiple cURL requests, we may encounter the situation where the same request is sent multiple times, which will cause the same cURL handle to exist repeatedly, wasting system resources. To solve this problem, we need to ensure that each requested handle can be closed in time after completion.

  1. Clean up cURL handles : Whenever a request is completed, use curl_multi_remove_handle to remove the handle of the request from the multiple handles, and then use curl_close to close the handle.

  2. Use curl_multi_close : After all requests have been executed, use curl_multi_close to close the multiple cURL handle. This prevents the script from still remaining useless handles after completing all tasks.

In this way, repeated request handles can be effectively avoided occupancy of resources and improved program performance.

5. Summary

The curl_multi_close function is an important function in the PHP cURL library that is used to clean up handles for multiple requests. Correct use of curl_multi_close can not only free up system resources, but also improve program performance and avoid memory leaks and resource waste.

Reasonable resource management is crucial when concurrent requests. After each request is completed, the repeated handles are cleaned in time to ensure that the resources requested are released in time, which will greatly improve the program's response speed and stability.