Current Location: Home> Latest Articles> curl_multi_close Common pitfalls when used with curl_getinfo

curl_multi_close Common pitfalls when used with curl_getinfo

gitbox 2025-05-29

The curl_multi_* function provides good support when using cURL extensions for concurrent HTTP requests in PHP. In particular, the two functions curl_multi_close and curl_getinfo often need to be used together to handle the responses of multiple requests. However, in actual development, developers may encounter some pitfalls, especially when using curl_getinfo to get the response information for each request. If you are not careful, it may lead to unexpected errors or the correct information cannot be obtained.

1. Resource issues after using curl_multi_close

curl_multi_close is used to close multiple cURL session handles. After the request is completed, you need to call this function to free all resources. If you use curl_getinfo to get the detailed information of a request before closing the session, you may encounter a problem: after closing the session, the resources associated with these session handles are destroyed, and you cannot get the correct response information by calling curl_getinfo .

 // Sample code:The wrong way
$mh = curl_multi_init();
$ch1 = curl_init("https://gitbox.net/api/v1/resource1");
$ch2 = curl_init("https://gitbox.net/api/v1/resource2");

curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($active);

// mistake:Called before closing the session handle curl_getinfo
$info1 = curl_getinfo($ch1);  // An error will occur here,Because the session has been closed
$info2 = curl_getinfo($ch2);

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

In the above example, we called curl_getinfo before curl_multi_close , and the returned information is no longer available and may even lead to PHP errors.

2. The correct way: get information first, then close the session

To avoid the above problems, you should use curl_getinfo to get the response information for each request before calling curl_multi_close . Here is a correct demonstration after the modification:

 // The correct way to do it
$mh = curl_multi_init();
$ch1 = curl_init("https://gitbox.net/api/v1/resource1");
$ch2 = curl_init("https://gitbox.net/api/v1/resource2");

curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($active);

// correct:Get response information before closing the session
$info1 = curl_getinfo($ch1);  
$info2 = curl_getinfo($ch2);

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

// Now you can use it safely $info1 and $info2 It's

In this modified version, we ensure that before closing all session handles, we get the response information for each request through curl_getinfo . This avoids attempting to access the already released resource after closing the session handle, causing an error to occur.

3. Loop problems when multiple requests

If you send multiple requests at the same time and want to get response information for each request, you will usually use a loop. At this time, you need to make sure that each requested curl_getinfo is obtained before curl_multi_close , otherwise resource loss will occur.

 // correct的做法:Use a loop to get information for each request one by one
$mh = curl_multi_init();
$channels = [];
$urls = [
    "https://gitbox.net/api/v1/resource1",
    "https://gitbox.net/api/v1/resource2"
];

foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_multi_add_handle($mh, $ch);
    $channels[] = $ch;
}

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($active);

foreach ($channels as $ch) {
    $info = curl_getinfo($ch);  // Get information one by one here
    // deal with $info
}

foreach ($channels as $ch) {
    curl_multi_remove_handle($mh, $ch);
}

curl_multi_close($mh);

In this example, we use an array to store each cURL session handle and get the response information for each request one by one before curl_multi_close . This approach avoids the problems mentioned earlier.

4. Pay attention to the order in concurrent requests

Sometimes, not all requests will be completed at the same time. To avoid calling curl_getinfo on unfinished requests, you can use the curl_multi_select function to detect which requests have been completed, making sure you only call curl_getinfo on completed requests.

 // More complex situations:use curl_multi_select deal with完成的请求
$mh = curl_multi_init();
$channels = [];
$urls = [
    "https://gitbox.net/api/v1/resource1",
    "https://gitbox.net/api/v1/resource2"
];

foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_multi_add_handle($mh, $ch);
    $channels[] = $ch;
}

do {
    $mrc = curl_multi_exec($mh, $active);
    if ($active) {
        curl_multi_select($mh);  // Wait for a request to complete
    }
} while ($active);

foreach ($channels as $ch) {
    $info = curl_getinfo($ch);  // Get the completed request information here
    // deal with $info
}

foreach ($channels as $ch) {
    curl_multi_remove_handle($mh, $ch);
}

curl_multi_close($mh);

Using curl_multi_select ensures that information is obtained in time when the request is completed, thereby avoiding data loss due to waiting or timeout issues.

Summarize

When using the curl_multi_* function in PHP, curl_getinfo and curl_multi_close are commonly used tools. The key to avoiding common pitfalls is to ensure that the response information for each session has been obtained through curl_getinfo before closing multiple sessions. With the right code structure and request management, you can ensure the stability and correctness of your program.