I need to download multiple files with Guzzle using concurrent requests so i choose to use Pool.
Basically, I wrote a function that receives a list of URL that are the remote files i need to download and a path which is the directory where i want to save the files. The function should save in the provided directory each file I'm sending the request to but instead the directory reamins empty and no file as been wrote inside it.
What am I doing wrong?
Here is my function with some echos for debugging purpose:
function async_multiple_files_download($files_url_list, $path) {
$client = new \GuzzleHttp\Client();
$requests = array();
for ($i = 0; $i < sizeof($files_url_list); $i++) {
$file_name = basename($files_url_list[$i]);
$request_destination_file_path = $path . DIRECTORY_SEPARATOR . $file_name;
$requests[$i] = new GuzzleHttp\Psr7\Request('GET', $files_url_list[$i], ['sink' => $request_destination_file_path]);
echo "Downloading " . basename($files_url_list[$i]) . "<br>from($files_url_list[$i])<br>to $request_destination_file_path" . "<br><br><br>";
}
$pool = new \GuzzleHttp\Pool($client, $requests, [
'concurrency' => 10,
'fulfilled' => function (\Psr\Http\Message\ResponseInterface $response, $index) {
echo 'success: '.$response->getStatusCode()."<br>";
},
'rejected' => function ($reason, $index) {
echo 'failed: '.$reason."<br>";
},
]);
$promise = $pool->promise();
$promise->wait();
}