In PHP, curl_multi_exec and curl_multi_info_read are very practical functions if you need to initiate multiple HTTP requests at the same time and want these requests to be executed efficiently in parallel. This article will introduce in detail how to use these two functions to achieve efficient parallel requests and give specific code examples.
curl_multi_exec : is used to execute a set of cURL handles to implement multiplexing. It executes all join requests in parallel.
curl_multi_info_read : Used to obtain information about completed requests, and can detect whether the request has ended, thereby processing the corresponding result.
Initialize multiple single cURL requests (curl handles).
Create a multiplexed handle.
Add all individual requests to the multiplexed handle.
Call curl_multi_exec to start executing all requests.
Continuously detect whether a request has been completed through curl_multi_info_read .
Process the result of the completion request.
Clean up resources.
<?php
// To requestURLList
$urls = [
'http://gitbox.net/api/endpoint1',
'http://gitbox.net/api/endpoint2',
'http://gitbox.net/api/endpoint3',
];
// initialization multi handle
$multiHandle = curl_multi_init();
$curlHandles = [];
// 1. Create a singlecurlHandle,And joinmultiHandle
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return results instead of direct output
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
// 2. Perform all requests
$running = null;
do {
// Execute a request
curl_multi_exec($multiHandle, $running);
// Optional:Waiting for an active connection,preventCPUToo high occupancy
curl_multi_select($multiHandle);
// 3. Check if there is a completed request
while ($info = curl_multi_info_read($multiHandle)) {
if ($info['msg'] === CURLMSG_DONE) {
$handle = $info['handle'];
// Get content
$content = curl_multi_getcontent($handle);
// Processing return content,For example, printing or parsing
echo "Request complete,Content length:" . strlen($content) . "\n";
// 移除完成的Handle,Free up resources
curl_multi_remove_handle($multiHandle, $handle);
curl_close($handle);
}
}
} while ($running > 0);
// 4. closuremultiHandle
curl_multi_close($multiHandle);
?>
curl_multi_init() : Initialize the multiplexed handle.
curl_multi_add_handle() : Adds each individual requested curl handle to the multi handle.
curl_multi_exec() : executes all requests.
curl_multi_select() : Wait for active connection to avoid idling of CPU.
curl_multi_info_read() : reads the relevant information of the completion request and determines whether the request has ended.
curl_multi_remove_handle() and curl_close() : Clean up resources to avoid memory leaks.
Loop until all requests are completed ( $running becomes 0).
The number of parallel requests should not be too large to avoid excessive server pressure or system resource bottlenecks.
Use curl_multi_select() to wait to prevent the program from falling into high CPU usage.
When processing returned content, pay attention to error checking to ensure that the request is successful.
If you need to initiate complex requests such as POST, header, and authentication, you can set corresponding options when initializing a single curl handle.