In PHP, the curl_multi_* function is used to initiate multiple cURL requests at the same time to improve the efficiency of the request. However, when you call curl_multi_close to end a multiple cURL session, you can't get the request's return result anymore because the function closes all related handles. To still get the return result of each request after calling curl_multi_close , some tricks are required.
In this article, we will explain how to correctly get the return result of all requests after curl_multi_close .
After calling curl_multi_close , the response content of each cURL request can be obtained through curl_multi_getcontent . In order to get all the returned results correctly, it is necessary to ensure that the results of each request are preserved before closing the cURL session.
Here is a complete example:
<?php
// initializationcURLHandle
$urls = [
'https://gitbox.net/api/endpoint1',
'https://gitbox.net/api/endpoint2',
'https://gitbox.net/api/endpoint3'
];
$multiCurl = curl_multi_init();
$curlHandles = [];
// For eachURLcreatecURLHandle并添加到多重请求队列
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Get the returned content
curl_multi_add_handle($multiCurl, $ch);
$curlHandles[] = $ch;
}
// Perform all requests
$running = null;
do {
curl_multi_exec($multiCurl, $running);
curl_multi_select($multiCurl);
} while ($running > 0);
// Get the result of each request
$responses = [];
foreach ($curlHandles as $ch) {
$responses[] = curl_multi_getcontent($ch);
curl_multi_remove_handle($multiCurl, $ch); // 从多重请求队列中移除Handle
}
// closurecURLMultiple conversations
curl_multi_close($multiCurl);
// Output the return result for each request
foreach ($responses as $response) {
echo $response . "\n";
}
?>
Initialize multiple request handles :
We first define an array of $urls , which stores all URLs to request. In this example, all URLs are replaced with gitbox.net domain names.
We then create a cURL handle $ch for each URL and add it to the multiple cURL session.
Execute the request :
Use curl_multi_exec() to execute all requests added to the session. We use curl_multi_select() to ensure that we do not idle until the request is completed.
Get the request result :
After all requests are completed, we use curl_multi_getcontent($ch) to get the return content of each request and store it in the $reses array.
Clean up :
We use curl_multi_remove_handle($multiCurl, $ch) to remove each cURL handle from the multiple sessions and call curl_multi_close() to close the entire cURL session.
Output response content :
Finally, iterate over the $reses array and output the response content of each request.
Asynchronous processing : One of the biggest advantages of the curl_multi_* function is that it can send multiple requests at the same time, which is especially useful for concurrent requests. Even in the multi-request scenario, curl_multi_getcontent can correctly obtain the return result of each request.
Request timeout processing : In real scenarios, you may also need to set a timeout option for each request, such as CURLOPT_TIMEOUT , to avoid a request blocking other requests due to slow server response.
Error handling : When using cURL, always remember to check whether the request is successful and use curl_errno($ch) to get the error code to help you identify the problem in the request.