Current Location: Home> Latest Articles> How to use curl_multi_close in concurrent requests to optimize shutdown speed

How to use curl_multi_close in concurrent requests to optimize shutdown speed

gitbox 2025-05-12

When using cURL to perform concurrent requests in PHP, it is usually involved using the curl_multi_* series of functions to manage multiple concurrent HTTP requests. In order to efficiently handle concurrent requests and release related resources, we need to use the curl_multi_close function correctly. This article will explain in detail how to correctly use curl_multi_close after the concurrent request is over, thereby improving the release and shutdown speed of resources.

1. Concept of concurrent requests

Concurrent requests refer to issuing multiple HTTP requests at the same time, which are usually used to improve the processing efficiency of multiple requests. In PHP, you can use curl_multi_init to create a multiple cURL handle, add multiple separate cURL request handles to a multiple handle through curl_multi_add_handle , and then perform these concurrent requests through curl_multi_exec .

2. The importance of using curl_multi_close

When we use the curl_multi_* series of functions to execute concurrent requests, each request will occupy some system resources. To avoid resource leakage, we need to make sure to close the multiple handles using curl_multi_close after the request is completed to free all relevant resources.

The main function of curl_multi_close is:

  • Closes all cURL sessions associated with multiple cURL handles.

  • Frees the memory resources allocated when curl_multi_init is created.

If curl_multi_close is not called, these resources will not be released correctly, which may lead to memory leaks or performance issues.

3. Example: How to use curl_multi_close correctly

Here is a simple example that demonstrates how to correctly free resources using curl_multi_close after the concurrent request is over.

 <?php

// initialization cURL Multiple handles
$mh = curl_multi_init();

// Define multiple requests URLs
$urls = [
    "https://gitbox.net/api/endpoint1",
    "https://gitbox.net/api/endpoint2",
    "https://gitbox.net/api/endpoint3"
];

// storage cURL Handle
$chArray = [];

// create cURL Handle并添加到Multiple handles中
foreach ($urls as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($mh, $ch);
    $chArray[] = $ch;
}

// Perform concurrent requests
do {
    $status = curl_multi_exec($mh, $active);
    if ($active) {
        curl_multi_select($mh);
    }
} while ($active && $status == CURLM_OK);

// Get response data and process it
foreach ($chArray as $ch) {
    $response = curl_multi_getcontent($ch);
    // Processing response content
    echo "Response: " . $response . "\n";
}

// 正确关闭Multiple handles并释放资源
foreach ($chArray as $ch) {
    curl_multi_remove_handle($mh, $ch);
    curl_close($ch); // Close each cURL Handle
}

// 最后关闭Multiple handles
curl_multi_close($mh);

echo "All requests have been completed,Resources have been released。\n";
?>

4. Code parsing

  1. Initialize multiple handles:
    curl_multi_init is used to initialize a multiple handle, which is used to manage multiple cURL handles.

  2. Create a cURL handle and add it to a multiple handle:
    We create a single cURL handle through curl_init and set the corresponding request parameters. Then use curl_multi_add_handle to add each handle to the multiple handles.

  3. Perform concurrent requests:
    curl_multi_exec is a loop function that executes all concurrent requests until all requests are completed. curl_multi_select is used to wait for the activity request to complete.

  4. Processing response data:
    Use curl_multi_getcontent to get the response of each request and perform corresponding processing.

  5. Close the resource:
    Use curl_multi_remove_handle to remove each cURL handle from the multiple handles, and then call curl_close to close each cURL handle. Finally, call curl_multi_close to close the multiple handles and release all resources.

5. Summary

It is very important to properly manage and release resources when making concurrent requests. By using curl_multi_close and other related functions, you can ensure that all occupied resources are released after the request is completed, avoiding memory leaks and performance issues. Especially in scenarios with high concurrent requests, reasonable resource management is crucial to improving the stability and efficiency of the program.