Current Location: Home> Latest Articles> How to correctly close multiple CURL connections using curl_multi_close

How to correctly close multiple CURL connections using curl_multi_close

gitbox 2025-05-12

In PHP, the curl_multi_* function family is used to execute multiple cURL requests simultaneously. curl_multi_close() is an important function in this series to close multiple cURL handles initialized by curl_multi_init() .

What is the curl_multi_* function series?

The curl_multi_* function family is a tool in PHP for processing multiple cURL requests in parallel. They allow you to send multiple HTTP requests simultaneously without waiting for each request to complete before sending the next one. In this way, multiple requests can be processed significantly, especially when multiple APIs or websites need to be requested.

Common curl_multi_* functions are:

Why use curl_multi_close() ?

When multiple cURL requests are executed using the curl_multi_* function, each request is independent and each request has its own resource consumption. In order to ensure that these resources can be released in time after the request is completed, curl_multi_close() must be called to close the connection.

If you don't use curl_multi_close() , these cURL handles may continue to take up memory and other resources, causing performance issues and even memory leaks. Therefore, it is very important to properly close these connections after the request is complete.

curl_multi_close() usage example

Here is an example showing how to use curl_multi_close() to close multiple cURL connections.

 <?php
// Initialize multiple cURL Handle
$mh = curl_multi_init();

// Define what to request URL List
$urls = [
    "https://gitbox.net/api/endpoint1",
    "https://gitbox.net/api/endpoint2",
    "https://gitbox.net/api/endpoint3"
];

// storage cURL Handle
$ch_handles = [];

// Create each cURL Handle并添加到 multi Handle中
foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($mh, $ch);
    $ch_handles[] = $ch;
}

// Perform multiple requests
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running > 0);

// Get the content of each request and close it cURL Handle
foreach ($ch_handles as $ch) {
    $content = curl_multi_getcontent($ch);
    echo $content . "\n"; // Output the returned content
    curl_multi_remove_handle($mh, $ch);
    curl_close($ch); // Close each cURL Handle
}

// closure multi Handle
curl_multi_close($mh);
?>

Code parsing

  1. Initialize multiple cURL handles :

    • Use curl_multi_init() to create a new multi handle that manages multiple cURL requests.

  2. Add multiple cURL handles :

    • Create a cURL handle for each URL, set relevant options, and then add these handles to the multi handle via curl_multi_add_handle() .

  3. Execute the request :

  4. Get the content and close the handle :

  5. Close the multi handle :

    • Finally, call curl_multi_close() to close the multi handle and free all resources.

summary

Correct use of curl_multi_close() is a key step to ensure that multiple cURL request resources are released in a timely manner. When processing multiple requests in parallel, remember to call curl_multi_close() after each request is executed to close all relevant cURL connections. This not only prevents memory leaks, but also improves the efficiency of the code.