Current Location: Home> Latest Articles> curl_multi_close data synchronization problem when used with curl_getinfo

curl_multi_close data synchronization problem when used with curl_getinfo

gitbox 2025-05-12

In PHP, the curl_multi_* series of functions provide the ability to process multiple cURL requests asynchronously, which is useful for scenarios where multiple URLs are requested at the same time and waiting for results. In these asynchronous requests, curl_getinfo is used to obtain detailed information about each request, such as the HTTP status code, the requested URL, the transmission time, etc. However, when closing a multi-cURL session using curl_multi_close , how to correctly obtain and process the return information of each request to avoid data synchronization issues has become a common problem for developers in actual development.

This article will discuss how to ensure that the detailed information of the request is correctly obtained through curl_getinfo when processing multiple requests using curl_multi_close , and avoid data synchronization issues caused by improper request closing timing.

1. Understand the basic usage of curl_multi_* functions and curl_getinfo

In PHP, the curl_multi_* function allows us to initiate multiple cURL requests simultaneously and process them in parallel through multiple threads. For example, use curl_multi_init to initialize a cURL handle set, use curl_multi_add_handle to add multiple cURL handles, then execute via curl_multi_exec and wait for all requests to complete. Finally, close the session using curl_multi_close .

Here is a simple example:

 $mh = curl_multi_init();  // initialization multi curl Handle
$ch1 = curl_init("http://gitbox.net/api/data1");  // ask 1
$ch2 = curl_init("http://gitbox.net/api/data2");  // ask 2

curl_multi_add_handle($mh, $ch1);  // 添加ask 1
curl_multi_add_handle($mh, $ch2);  // 添加ask 2

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

curl_multi_remove_handle($mh, $ch1);  // 移除ask 1
curl_multi_remove_handle($mh, $ch2);  // 移除ask 2

curl_multi_close($mh);  // closure multi curl Handle

2. The root cause of data synchronization problems

When multiple requests are executed simultaneously, we usually want to be able to get their details after all requests are completed. The curl_getinfo function is used to obtain execution information for each cURL request, such as the returned status code ( CURLINFO_HTTP_CODE ) and other metadata. However, since multiple requests are made asynchronously and in parallel, curl_getinfo needs to be called at the appropriate time to ensure that data is not retrieved before the request has been fully executed.

If you call curl_getinfo before the request is complete, you may get wrong or incomplete data. Therefore, reasonable timing of synchronization is very important.

3. Correct usage order and synchronization policy

3.1 Handle concurrent requests through curl_multi_select

When you call curl_multi_exec to perform multiple requests, PHP will enter a loop until all requests are executed. In this process, curl_multi_select can also help us handle event waiting, allowing us to better control when to get the result of the request.

 do {
    // 等待ask完成
    $mrc = curl_multi_exec($mh, $active);

    // Wait until data is ready
    if ($active) {
        curl_multi_select($mh);
    }
} while ($active && $mrc == CURLM_OK);

3.2 Make sure to get data after removing the handle

Once all requests are completed and all handles are removed, it is the right time to obtain the request information. Otherwise, if you close the handle in advance, you may miss the result of some requests.

 // 获取ask信息
$info1 = curl_getinfo($ch1);
$info2 = curl_getinfo($ch2);

// Output information
echo "Request 1 HTTP Code: " . $info1['http_code'] . "\n";
echo "Request 2 HTTP Code: " . $info2['http_code'] . "\n";

3.3 Use curl_multi_remove_handle to remove the handle

After getting the data, remember to remove each handle from the curl_multi session and close them at the end. This ensures that no more attempts are made to get the results of the closed request.

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

4. Code example: Comprehensive application

Combined with the aforementioned, here is a more complete example showing how to get the details of each request correctly and ensure there are no data synchronization issues:

 $mh = curl_multi_init();  // initialization multi curl Handle
$ch1 = curl_init("http://gitbox.net/api/data1");  // ask 1
$ch2 = curl_init("http://gitbox.net/api/data2");  // ask 2

curl_multi_add_handle($mh, $ch1);  // 添加ask 1
curl_multi_add_handle($mh, $ch2);  // 添加ask 2

// 执行ask并等待完成
do {
    $mrc = curl_multi_exec($mh, $active);
    if ($active) {
        curl_multi_select($mh);  // Wait for the data to be ready
    }
} while ($active && $mrc == CURLM_OK);

// 获取ask的执行信息
$info1 = curl_getinfo($ch1);
$info2 = curl_getinfo($ch2);

// 输出ask信息
echo "Request 1 HTTP Code: " . $info1['http_code'] . "\n";
echo "Request 2 HTTP Code: " . $info2['http_code'] . "\n";

// 移除Handle
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);

// closure multi curl Handle
curl_multi_close($mh);

in conclusion

By correctly using curl_multi_exec and curl_multi_select , and ensuring that information is obtained through curl_getinfo after the request is completed, data synchronization problems caused by asynchronous requests can be effectively avoided. For the processing of concurrent requests, the timing is crucial. A reasonable call order and synchronization strategy can ensure that the correct data is obtained.