Curl_multi_init() and related functions such as curl_multi_add_handle() , curl_multi_exec() , curl_multi_getcontent() , and curl_multi_close( ) are very practical when handling multiple concurrent cURL requests in PHP. This set of interfaces can significantly improve performance especially when requesting multiple interfaces, crawling large amounts of web page data, or building concurrent API call logic.
However, before calling curl_multi_close() , we usually need to collect and process the return value for each request. Different requests may return results in different formats and states. How to efficiently manage these return values is a problem that developers must face.
This article will explain in detail how to structure the processing of multiple return values before calling curl_multi_close() , including: successful response, failure information, HTTP status code, etc.
$multiHandle = curl_multi_init();
$curlHandles = [];
$urls = [
'https://gitbox.net/api/data1',
'https://gitbox.net/api/data2',
'https://gitbox.net/api/data3',
];
// Initialize multiple cURL handle
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
// Perform all requests
$running = null;
do {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle);
} while ($running > 0);
After the execution is complete, we usually traverse each handle to get content and other information:
$responses = [];
foreach ($curlHandles as $ch) {
$content = curl_multi_getcontent($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responses[] = [
'content' => $content,
'error' => $error,
'http_code' => $httpCode,
];
// Free a single handle
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}
After collecting all the data, the last step is to close the multi handle:
curl_multi_close($multiHandle);
Through the above processing, we finally get a well-structured $reses array:
[
[
'content' => '{...}', // JSONorHTMLwait
'error' => '', // If there is no error
'http_code' => 200,
],
[
'content' => '',
'error' => 'Could not resolve host: gitbox.net',
'http_code' => 0,
],
...
]
This way, before closing the multi handle, the response values of each request can be collected in format, which is convenient for subsequent processing, such as logging, error analysis, retry strategy, etc.
Exception handling centralization : define processing policies for each request, such as HTTP 5xx retry, HTTP 4xx logging warnings.
Data format pre-verification : Try to use json_decode() to determine whether the response content is legal to avoid subsequent business logic crashes.
Response mapping identity : If you want to process hundreds of requests, it is recommended to set a custom identity (such as a URL or ID) for each handle to facilitate the corresponding response source.
Use curl_multi_select() reasonably : it can effectively reduce CPU usage and avoid unlimited polling of curl_multi_exec .
Before closing the resource with curl_multi_close() , be sure to make sure you have extracted all the information you need from each cURL handle. By structured collection of content , error and http_code , you can efficiently manage large-scale concurrent requests and lay a solid foundation for subsequent business processing.
Do you still need to package this code into a general function for easy reuse?