Current Location: Home> Latest Articles> Use curl_multi_close in the correct order with curl_multi_init

Use curl_multi_close in the correct order with curl_multi_init

gitbox 2025-05-12

In PHP, cURL is a powerful and commonly used network request tool, and the curl_multi_* series of functions allow us. This is very useful in scenarios such as high-performance interface aggregation, asynchronous requests, etc.

However, many developers often ignore the resource release process when using curl_multi_* , especially the use of curl_multi_close() . This article will take the standard process as the main line and will take you to understand the complete usage of curl_multi_init to curl_multi_close step by step, and will be supplemented with code examples.

1. What is the curl_multi series function?

The curl_multi_* series of functions provided by PHP are tools for managing multiple concurrent curl handles . This is different from the traditional single curl request, which allows you to initiate multiple HTTP requests at the same time, improving response efficiency.

Using these functions can be roughly divided into the following steps:

  1. Initialize multi handle: curl_multi_init()

  2. Add multiple curl requests (easy handle): curl_multi_add_handle()

  3. Execute requests: curl_multi_exec() and curl_multi_select()

  4. Get the response content: curl_multi_getcontent()

  5. Remove and close easy handle: curl_multi_remove_handle() and curl_close()

  6. Close multi handle: curl_multi_close()

2. Standard process explanation and code examples

Here is a standard multi-request process, assuming we need to request three different URLs concurrently:

 <?php

// To request URL List
$urls = [
    "https://gitbox.net/api/service1",
    "https://gitbox.net/api/service2",
    "https://gitbox.net/api/service3"
];

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

// 1. For each URL Create a curl easy handle and add to multi handle middle
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;
}

// 2. implement multi ask
$active = null;
do {
    $mrc = curl_multi_exec($multiHandle, $active);
    // Waiting for the Internet I/O Get ready,avoid CPU Too high occupancy
    if ($mrc == CURLM_OK) {
        curl_multi_select($multiHandle);
    }
} while ($active > 0 && $mrc == CURLM_OK);

// 3. Get content and clean handles
$responses = [];
foreach ($curlHandles as $i => $ch) {
    // Get the response content
    $responses[$i] = curl_multi_getcontent($ch);

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

// 4. Last Close multi handle,Free up resources
curl_multi_close($multiHandle);

// Output result
foreach ($responses as $index => $body) {
    echo "Response from URL #{$index}: \n";
    echo $body . "\n\n";
}

3. Why is curl_multi_close important?

Although curl_multi_close() does not destroy the easy handle immediately (you need to remove and close them first), it is responsible for freeing the underlying resources occupied by the multi handle itself . If you forget to call it, it can cause memory leaks or file descriptor exhaustion in long-running scripts such as resident processes.

Summary: Any script that uses curl_multi_init() must end with curl_multi_close() !

4. Frequently Asked Questions

  1. Request blocking and stuck?

    • Check if curl_multi_select() is used correctly.

    • Make sure that curl_setopt(..., CURLOPT_RETURNTRANSFER, true) is not missed.

  2. Memory leak or connection exception?

5. Summary

curl_multi_* provides a very efficient means when processing network requests concurrently. Mastering the complete process from curl_multi_init() to curl_multi_close() can effectively improve code performance and resource management capabilities.

Remember these three "must":

  • Each request must be added to the multi handle;

  • Each request must be removed and closed upon completion;

  • Finally, curl_multi_close() must be called to free the multi resource.

Through standardized process and resource management, your PHP concurrent requests will be more robust and efficient.