When using PHP's curl_multi_* series functions for concurrent requests, if handled improperly, it is easy to send some invalid or incorrect requests before curl_multi_close() call, resulting in wasted resources and even server errors. In order to ensure that all requests are valid and correct before closing multi-handle, you can start with the following aspects to optimize.
Each individual cURL request (the handle initialized by curl_init ) should be fully checked before being added to curl_multi_add_handle , such as ensuring that the URL is formatted correctly, the necessary request headers are set intact, and the timeout parameters are reasonable.
Sample code:
function createCurlHandle(string $url): ?\CurlHandle
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return null;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Set timeout
return $ch;
}
During multi-threaded execution, it is necessary to loop through the execution status to ensure that all handles respond correctly, rather than blindly closing the connection quickly.
Example:
$urls = [
"https://gitbox.net/api/data1",
"https://gitbox.net/api/data2",
"https://gitbox.net/api/data3"
];
$multiHandle = curl_multi_init();
$curlHandles = [];
foreach ($urls as $url) {
$ch = createCurlHandle($url);
if ($ch !== null) {
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
}
$running = null;
do {
$status = curl_multi_exec($multiHandle, $running);
if ($status > CURLM_OK) {
// Recording errors,Or interrupt as needed
break;
}
// allowCPUHave a break
curl_multi_select($multiHandle);
} while ($running > 0);
Before formally calling curl_multi_close() , you should traverse all individual handles, confirm their return code ( CURLINFO_HTTP_CODE ) and other information, and decide whether you need to try again or mark as failed.
foreach ($curlHandles as $ch) {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
// Handle exceptions,For example, logging、Re-submit request, etc.
echo "Request failed,HTTPStatus code:" . $httpCode . PHP_EOL;
}
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}
curl_multi_close($multiHandle);
In a scenario where multiple request concurrency is performed, it is extremely important to correctly manage each request life cycle. From strictly verifying parameters starting from the initialization phase, monitoring the status during execution, and thoroughly checking the return value before the end, this series of steps can minimize resource waste and server-side pressure caused by wrong requests. By rationally using curl_multi_select() with exception handling mechanism, your curl_multi calls can be both efficient and stable, improving the reliability of the overall system.