Current Location: Home> Latest Articles> How to correctly release resources after curl_multi_close

How to correctly release resources after curl_multi_close

gitbox 2025-05-12

In PHP, cURL is a powerful library that can help us send HTTP requests and get responses. Using curl_multi_* function sets, especially curl_multi_close , multiple cURL requests can be processed concurrently. When we complete the operation on multiple cURL handles, closing the handle using curl_multi_close is an important step. However, if we do not release the relevant resources correctly, it may lead to memory leaks and errors.

This article will introduce how to correctly release resources after using the curl_multi_close function, thereby avoiding memory leaks and errors.

1. Overview of cURL Multiple Request Processing

In PHP, the curl_multi_* function allows you to process multiple cURL requests simultaneously. curl_multi_init is used to initialize multiple cURL handles, curl_multi_add_handle is used to add each individual cURL handle to the multiple cURL handle, and curl_multi_exec is used to perform all requests. After all requests are completed, we use curl_multi_close to close these handles.

Sample code:

 $multiCurl = curl_multi_init();
$curlHandles = [];

for ($i = 0; $i < 3; $i++) {
    $curlHandles[$i] = curl_init("https://gitbox.net/api/data?id=$i");
    curl_setopt($curlHandles[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiCurl, $curlHandles[$i]);
}

$running = null;
do {
    curl_multi_exec($multiCurl, $running);
} while ($running);

foreach ($curlHandles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo $response . "\n";
}

// Close all handles
curl_multi_close($multiCurl);

2. Release resources correctly after using curl_multi_close

After using curl_multi_close , although it closes the multiple cURL handles, the individual cURL handles remain until they are explicitly released. If we do not release these handles, they will continue to take up memory, which may lead to memory leaks.

To ensure that resources are properly released, curl_close should be explicitly called after closing the multiple cURL handles to free each individual cURL handle.

 // Close each cURL Handle
foreach ($curlHandles as $ch) {
    curl_close($ch);
}

Doing this ensures that after curl_multi_close , each cURL handle is also correctly released, avoiding memory leaks.

3. Error handling and resource release

An error may be encountered while executing multiple cURL requests. When handling these errors, it is important to ensure that the cURL handle is properly closed when the error occurs. Even if an error occurs, you should always clean up resources at the end.

Error handling code example:

 $multiCurl = curl_multi_init();
$curlHandles = [];

for ($i = 0; $i < 3; $i++) {
    $curlHandles[$i] = curl_init("https://gitbox.net/api/data?id=$i");
    curl_setopt($curlHandles[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiCurl, $curlHandles[$i]);
}

$running = null;
do {
    $execReturn = curl_multi_exec($multiCurl, $running);
    
    if ($execReturn !== CURLM_OK) {
        echo "Error occurred while executing multi requests.\n";
        break;
    }
} while ($running);

foreach ($curlHandles as $ch) {
    if (curl_errno($ch)) {
        echo "cURL error: " . curl_error($ch) . "\n";
    } else {
        $response = curl_multi_getcontent($ch);
        echo $response . "\n";
    }
    curl_close($ch);
}

curl_multi_close($multiCurl);

This code ensures that even if an error occurs, all cURL handles are released, thus avoiding resource leakage.

4. Summary

  1. After using curl_multi_close , don't forget to close each cURL handle separately, using curl_close .

  2. When an error occurs, make sure that all resources are still freed and avoid memory leaks.

  3. curl_multi_close can only close multiple cURL handles, but cannot automatically close separate cURL handles. They must be cleaned manually.

Following these best practices can help you avoid memory leaks and errors and make your program more efficient and stable.