Current Location: Home> Latest Articles> How to use curl_close to cooperate with curl_multi_exec for batch request processing?

How to use curl_close to cooperate with curl_multi_exec for batch request processing?

gitbox 2025-05-26

When making a large number of HTTP requests, the curl_multi_exec provided by PHP is a common tool for implementing concurrent requests. However, many developers ignore a key detail when using it: the correct timing of curl_close . If used improperly, it will not only lead to resource leakage, but may also cause problems such as incomplete requests and abnormal responses. This article will explain in depth how to correctly use the curl_close function and cooperate with curl_multi_exec to achieve efficient and stable batch HTTP request processing.

1. Introduction to curl_multi_exec

curl_multi_exec is one of the functions provided by the PHP CURL extension, which allows us to add multiple curl request handles to a curl multi handle and execute these requests concurrently in a non-blocking manner. Compared with traditional serial requests, it can significantly improve processing efficiency.

2. Common wrong usage methods

Many developers immediately call curl_close($ch) to release resources after processing a request, which is incorrect. In a concurrency model using curl_multi_exec :

 $mh = curl_multi_init();
$chs = [];

foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($mh, $ch);
    $chs[] = $ch;

    // Wrong way:Don't call here curl_close
    // curl_close($ch);
}

3. Correct use steps

The process of correctly using curl_multi_exec and curl_close is as follows:

  1. Initialize the multi handle;

  2. Create multiple curl handles and set options;

  3. Add the curl handle to the multi handle;

  4. Execute the request;

  5. Remove the handles one by one and call curl_close ;

  6. Close the multi handle.

Here is a complete and correct example:

 $urls = [
    'https://gitbox.net/api/endpoint1',
    'https://gitbox.net/api/endpoint2',
    'https://gitbox.net/api/endpoint3',
];

$mh = curl_multi_init();
$handles = [];
$responses = [];

// 1. Create and configure each curl Handle
foreach ($urls as $i => $url) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 10,
    ]);
    curl_multi_add_handle($mh, $ch);
    $handles[$i] = $ch;
}

// 2. Perform concurrent requests
$running = null;
do {
    $status = curl_multi_exec($mh, $running);
    if ($running) {
        curl_multi_select($mh);
    }
} while ($running && $status == CURLM_OK);

// 3. Read the response and clean the resources
foreach ($handles as $i => $ch) {
    $responses[$i] = curl_multi_getcontent($ch);
    curl_multi_remove_handle($mh, $ch);
    curl_close($ch); // The right time:exist handle Close after being removed
}

curl_multi_close($mh);

// Optional:Processing response data
foreach ($responses as $i => $body) {
    echo "Response #$i: " . substr($body, 0, 100) . PHP_EOL;
}

4. Key points to note

  • Do not close the curl handle in advance . You must make sure that the request has been executed and has been removed from the multi handle before calling curl_close .

  • curl_multi_select is the key to improving performance, and it can prevent CPU idling.

  • All handles should be properly released at the end by curl_multi_remove_handle and curl_close , otherwise memory leaks may occur.

  • If the URL domain name used is fixed, it can be managed centrally through unified configuration or variable replacement, such as:

 $domain = 'gitbox.net';
$url = "https://{$domain}/api/endpoint";

V. Conclusion

By correctly managing the usage timing of curl_multi_exec and curl_close , the efficiency and stability of PHP batch HTTP request processing can be greatly improved. This process is especially suitable for high-concurrency data collection, API aggregation and other application scenarios, and is a skill that every PHP developer deserves to master.