cURL is a very powerful tool in PHP when making multiple asynchronous HTTP requests. Using the curl_multi_* function, developers can initiate multiple requests at the same time to improve efficiency. The curl_multi_close function is used to close multiple cURL sessions, but how do you ensure the order of responses when making asynchronous requests? Because curl_multi_exec may return responses for multiple requests, and the order in which these responses may be completed may be different from the order in which they are requested.
This article will explain in detail how to ensure that the results of asynchronous requests maintain the order of requests when using curl_multi_close .
The curl_multi_* series of functions provide a way to improve performance through concurrent requests. Several commonly used functions include:
curl_multi_init() : Initialize a cURL handle and create a cURL resource pool.
curl_multi_add_handle() : Adds a cURL handle to the multiple cURL request pool.
curl_multi_exec() : executes multiple cURL requests.
curl_multi_getcontent() : Get the response content of each request.
curl_multi_close() : Close the cURL request pool and release the resources.
These functions are often used together to perform asynchronous requests, thereby improving performance. However, since the order of completion of responses may be different from the order of requests, how to ensure the order of response results becomes an important issue.
When making asynchronous requests, curl_multi_exec will initiate multiple requests in the background at the same time and wait for all requests to complete. However, the completion time of these requests is unpredictable, so their response order may be disrupted.
To ensure that the results are processed in order, we need to use some mechanism to record the mapping relationship between the request and the response, a common practice is to use associative arrays. Specifically, the developer can set a unique identifier for each request and sort the results correctly according to this identifier when the response is received.
Here is an implementation example:
<?php
// To requestURLList
$urls = [
'https://gitbox.net/api/data1',
'https://gitbox.net/api/data2',
'https://gitbox.net/api/data3'
];
// initialization cURL multi Handle
$mh = curl_multi_init();
// askHandle数组
$curl_handles = [];
$responses = [];
$index = 0;
// Create for each request cURL Handle并添加到 multi Handle中
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Record the index of each request,So that the responses are processed in order
curl_multi_add_handle($mh, $ch);
// 将Handle与索引关联
$curl_handles[(string)$ch] = $index++;
}
// Perform all cURL ask
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh); // Let the program wait until data returns
} while ($running > 0);
// 读取每个ask的响应,并按ask的顺序保存结果
foreach ($curl_handles as $ch => $index) {
$response = curl_multi_getcontent($ch);
$responses[$index] = $response; // Save the response by index
curl_multi_remove_handle($mh, $ch); // 移除Handle
curl_close($ch); // 关闭每个Handle
}
// Last Close multi Handle
curl_multi_close($mh);
// Output response result,确保按照ask顺序输出
foreach ($responses as $response) {
echo $response . PHP_EOL;
}
?>
In the above code, we first initialize a cURL multi handle and create a cURL handle for each URL. To ensure that the responses are returned in order, we use an associative array $curl_handles , which associates each cURL handle with its index in the request list.
When all requests are completed, we iterate through this associative array and store each response in the $reses array according to the index, ensuring the order of the responses.
When all requests are executed using curl_multi_exec , the running parameter is used to indicate the number of requests that are still being executed. curl_multi_select will cause the program to block when no data is returned until a request is completed, ensuring that we do not waste CPU resources.
Finally, we use curl_multi_remove_handle and curl_close to close all cURL handles, and use curl_multi_close to close multiple cURL handles to free up related resources.
By using the associative arrays reasonably, we can ensure that when performing asynchronous requests, the response results are returned in the order of requests. When using the curl_multi_* series of functions for asynchronous requests in PHP, it is important to maintain order, especially when handling responses from multiple interfaces. Through this example, you can better understand how to make asynchronous requests in PHP and ensure order, improving program performance and stability.