Current Location: Home> Latest Articles> How to ensure that curl_multi_close does not interfere with the intermediate process of requests

How to ensure that curl_multi_close does not interfere with the intermediate process of requests

gitbox 2025-05-29

When using cURL extensions in PHP, the curl_multi_* function is usually used to handle multiple concurrent requests. These functions can significantly improve the performance of your program, especially when multiple HTTP requests are sent simultaneously. However, when using the curl_multi_close function, some developers may encounter problems of interference in request processing, resulting in request not being completed correctly or returning errors.

This article will explore how to avoid interference in request processing and share some practical code examples and solutions.

1. Overview of curl_multi_* Function

PHP provides the curl_multi_* function to perform multiple concurrent cURL requests. The main functions are:

Sample code

 <?php

$multiHandle = curl_multi_init();

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

$handles = [];

// Initialize multiple request handles
foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiHandle, $ch);
    $handles[] = $ch;
}

// Execute a request
$running = null;
do {
    curl_multi_exec($multiHandle, $running);
} while ($running);

// Get response data
foreach ($handles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo $response . "\n";
    curl_multi_remove_handle($multiHandle, $ch);
}

// Close the handle
curl_multi_close($multiHandle);

?>

2. Curl_multi_close issue

Although the curl_multi_close function is a convenient cleaning function, it can sometimes interfere at the last stage of request processing, especially when curl_multi_close is called when the request is not completed. This interference may cause some requests to fail to complete correctly or the received response is incomplete.

Common causes of interference

  1. The completion time of concurrent requests is inconsistent : Due to the different response times of different requests, some requests may not have been completed when curl_multi_close is called, resulting in the request being closed in advance.

  2. Resource Release Issues : In some cases, calling curl_multi_close before the resource is fully released may affect other requests or cause the connection to be disconnected.

3. How to avoid interference?

In order to avoid interference in the request processing of curl_multi_close function, the following strategies can be adopted:

3.1 Ensure that all requests are completed

Make sure that all requests are completed before calling curl_multi_close . You can confirm whether all requests have been completed by checking the running status returned by curl_multi_exec . curl_multi_close should be called only when all requests are processed.

Improved code example:

 <?php

$multiHandle = curl_multi_init();

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

$handles = [];

// Initialize multiple request handles
foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiHandle, $ch);
    $handles[] = $ch;
}

// Execute a request
$running = null;
do {
    $mrc = curl_multi_exec($multiHandle, $running);
    if ($mrc > 0) {
        echo "CURL exec error: " . curl_multi_strerror($mrc);
    }
} while ($running);

// Get response data
foreach ($handles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo $response . "\n";
    curl_multi_remove_handle($multiHandle, $ch);
}

// Close the handle
curl_multi_close($multiHandle);

?>

3.2 Use curl_multi_select to improve performance

In some cases, curl_multi_select() can be used to optimize the performance of request processing. Instead of constantly polling curl_multi_exec , it can block processes while waiting for the cURL request to complete, which reduces CPU usage and ensures smooth processing of requests.

Code example:

 <?php

$multiHandle = curl_multi_init();

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

$handles = [];

// Initialize multiple request handles
foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multiHandle, $ch);
    $handles[] = $ch;
}

// Execute a request并使用 curl_multi_select Improve performance
$running = null;
do {
    $mrc = curl_multi_exec($multiHandle, $running);
    if ($mrc > 0) {
        echo "CURL exec error: " . curl_multi_strerror($mrc);
    }
    curl_multi_select($multiHandle);
} while ($running);

// Get response data
foreach ($handles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo $response . "\n";
    curl_multi_remove_handle($multiHandle, $ch);
}

// Close the handle
curl_multi_close($multiHandle);

?>

4. Summary

To avoid interference in request processing, we need to ensure that all requests have been completed and the response has been returned correctly. You can monitor the execution status of the request, use curl_multi_select to optimize performance, and ensure that curl_multi_close is called after the request is completed to avoid possible interference.

Through these methods, the stability and performance of concurrent requests in PHP can be effectively improved, ensuring that the request processing process is smooth and there is no unnecessary interference.