Current Location: Home> Latest Articles> curl_multi_close Usage Example: How to Handle Concurrent HTTP Requests

curl_multi_close Usage Example: How to Handle Concurrent HTTP Requests

gitbox 2025-05-12

In modern web development, performance optimization has always been an important topic. Especially when multiple HTTP requests need to be initiated, how to efficiently manage concurrently directly affects the response speed of the program and the efficiency of server resources. PHP provides cURL extension, where the curl_multi_* series functions can help developers process multiple requests at the same time, greatly improving execution efficiency.

Let’s learn more about the usage of curl_multi_close , and combine it with actual examples to explain how to correctly manage concurrent HTTP requests.

In PHP, curl_multi_close() is a function used to close a cURL multi handle . When we create a multi handle using curl_multi_init() and add multiple separate cURL handles for concurrent processing through curl_multi_add_handle() , we finally need to call curl_multi_close() to free up resources to prevent memory leaks.

Basic usage steps

The usual complete process of concurrent requests is as follows:

  1. Initialize multi handle.

  2. Create multiple cURL easy handles and set the corresponding URL and options.

  3. Add easy handle to multi handle.

  4. Execute multi handle.

  5. Remove and close each easy handle.

  6. Finally close the multi handle (using curl_multi_close() ).

Sample code

Here is a complete example that demonstrates how to properly use curl_multi_close() to manage concurrent HTTP requests:

 <?php

// To be requested URL List
$urls = [
    'https://gitbox.net/api/data1',
    'https://gitbox.net/api/data2',
    'https://gitbox.net/api/data3',
];

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

// initialization每个 cURL easy handle,and add to multi handle
foreach ($urls as $i => $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

// implement multi handle
$running = null;
do {
    $status = curl_multi_exec($multiHandle, $running);
    if ($running) {
        // Waiting for an active connection
        curl_multi_select($multiHandle);
    }
} while ($running > 0);

// Processing returns results
foreach ($curlHandles as $ch) {
    $content = curl_multi_getcontent($ch);
    $info = curl_getinfo($ch);

    echo "ask URL: " . $info['url'] . PHP_EOL;
    echo "HTTP Status code: " . $info['http_code'] . PHP_EOL;
    echo "Return to content: " . substr($content, 0, 100) . "..." . PHP_EOL;
    echo str_repeat("-", 50) . PHP_EOL;

    // Remove and close easy handle
    curl_multi_remove_handle($multiHandle, $ch);
    curl_close($ch);
}

// closure multi handle
curl_multi_close($multiHandle);
?>

Key Notes

  • Be sure to call curl_multi_close() after all easy handles have been removed.

  • If you forget to call curl_multi_close() , the resource may be automatically released by PHP at the end of the script, but this is not a good habit, especially in long-term running or high-concurrency environments, which will cause memory leakage.

  • curl_multi_close() does not automatically close the added easy handle, so before closing the multi handle, each easy handle needs to be manually removed and closed.

summary

By using curl_multi_close() properly, it is possible to ensure that concurrent HTTP request management is both efficient and secure. Remember, after all requests are processed with curl_multi , be sure to clean up all handle resources to keep your program robust and performance.