Current Location: Home> Latest Articles> How to clean multiple concurrent requested resources using curl_multi_close

How to clean multiple concurrent requested resources using curl_multi_close

gitbox 2025-05-12

When handling a large number of concurrent HTTP requests, the curl_multi series functions are a very practical tool in PHP. Among them, curl_multi_close is an indispensable step in the final stage, used to correctly release resources created by curl_multi_init . This article will explain the correct usage of curl_multi_close and provide a practical example to help you avoid common resource leakage problems.

Why you need to use curl_multi_close

Whenever a curl multi handle is created using curl_multi_init , PHP allocates a piece of memory to manage the multi handle and the various requests it contains. Even if a single clone handle (generated by curl_init ) has been closed with curl_close , the overall multi handle will still occupy system resources.
If you forget to use curl_multi_close , it is easy to cause memory leakage and even program crashes in high concurrency scenarios.

Therefore, after all concurrent requests are completed, curl_multi_close must be called to completely free the resource .

Basic usage process

The general process of using the curl_multi series functions is as follows:

  1. Create multi handle ( curl_multi_init )

  2. Create and configure multiple separate curl handles ( curl_init )

  3. Add each curl handle to the multi handle ( curl_multi_add_handle )

  4. Execute and monitor all requests ( curl_multi_exec and curl_multi_select )

  5. Remove a single handle ( curl_multi_remove_handle ) and close ( curl_close )

  6. Finally close multi handle ( curl_multi_close )

Example: Concurrently request multiple interfaces and correctly clean up resources

Here is a complete example showing how to request multiple URLs at the same time and clean up all relevant resources correctly:

 <?php

// To send concurrent requestsURLList
$urls = [
    "https://gitbox.net/api/endpoint1",
    "https://gitbox.net/api/endpoint2",
    "https://gitbox.net/api/endpoint3",
];

// initialization multi handle
$multiHandle = curl_multi_init();
$curlHandles = [];

// initialization每个单独的 curl handle And join multi handle
foreach ($urls as $key => $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_multi_add_handle($multiHandle, $ch);
    $curlHandles[$key] = $ch;
}

// Execute a request
$running = null;
do {
    $status = curl_multi_exec($multiHandle, $running);
    if ($status > CURLM_OK) {
        // Error handling
        echo "cURL error: " . curl_multi_strerror($status);
        break;
    }

    // Waiting for an active connection
    curl_multi_select($multiHandle);
} while ($running > 0);

// Collect response results
$responses = [];
foreach ($curlHandles as $key => $ch) {
    $responses[$key] = curl_multi_getcontent($ch);
    // Remove and close each child handle
    curl_multi_remove_handle($multiHandle, $ch);
    curl_close($ch);
}

// Last Close multi handle
curl_multi_close($multiHandle);

// Output response
foreach ($responses as $index => $response) {
    echo "Response from URL {$urls[$index]}:" . PHP_EOL;
    echo $response . PHP_EOL . PHP_EOL;
}
?>

Things to note

  • Be sure to remove individual curl handles first , then close them, and finally close multi handles.

  • Do not directly close the multi handle and then operate the child handle , as it will cause unpredictable errors.

  • In high concurrency environments, it is recommended to add timeout control ( CURLOPT_TIMEOUT ) to prevent long-term blockage.

Summarize

curl_multi_close is the last step to ensure the reasonable release of resources. Especially in multi-concurrency scenarios, it is very important to develop good closing habits. Correctly cleaning each clause handle and finally closing the multi handle can effectively prevent resource leakage and improve the stability and performance of PHP applications.