can a code like this perform curl execution in parallel and in background, while the responses get handled in sequence on foreground?
<?php
$urls = array(...
);
$n_handles = 4;
$processResult=function ($content){
echo $content;
};
$handleError=function ($error){
print_r($error);
};
/**
* Perform multiple asynchronous HTTP requests.
*
* @param int $n_handles Number of concurrent handles.
* @param array $urls URLs to request.
* @param callable $processResult Callback for processing successful responses.
* @param callable $handleError Callback for handling errors.
* @return void
*/
function multi_request(int $n_handles, array $urls, callable $processResult, callable $handleError)
: void
{
$multiHandle = curl_multi_init();
$toProcess = 0;
while ($toProcess < $n_handles && !empty($urls)) {
$handle = curl_init();
if (!$handle || curl_setopt_array($handle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => array_pop($urls)
]) !== true) {
$handleError('Failed to initialize or set options for cURL handle');
continue;
}
if (curl_multi_add_handle($multiHandle, $handle) !== CURLM_OK) {
$handleError('Failed to add cURL handle to multi handle');
curl_close($handle);
continue;
}
$toProcess++;
}
// Process responses asynchronously.
$processResponses = function () use (
&$urls, &$toProcess, $multiHandle, $processResult, $handleError
) {
while ($info = curl_multi_info_read($multiHandle)) {
$handle = $info['handle'];
if ($info['result'] == CURLM_OK) {
$content = curl_multi_getcontent($handle);
$processResult($content);
} else {
$handleError(curl_error($handle));
}
if (empty($urls)) {
$toProcess--;
curl_multi_remove_handle($multiHandle, $handle);
curl_close($handle);
continue;
}
curl_reset($handle);
curl_setopt($handle, CURLOPT_URL, array_pop($urls));
}
};
while ($toProcess > 0) {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle);
$processResponses();
}
curl_multi_close($multiHandle);
}
multi_request($n_handles, $urls, $processResult, $handleError);
i need it to create multiple connection to the same site to download and upload a sequence of file.
My hope is to maximize bandwidth usage, should it work?
is there a better solution to achieve parallelism on php?