0

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();

    }
Andrix
  • 9
  • 1
  • You can try this answer https://stackoverflow.com/questions/31857143/guzzle-parallel-file-download-using-guzzles-poolbatch-and-sink-option/40467700 if it doesn't work for you then we can debug the code snippet you have provided – bhucho Nov 07 '20 at 17:22
  • @bhucho Thanks for the answer. Unfortunately I already came across the question you linked and I already tried to follow it with no results :( – Andrix Nov 07 '20 at 18:05

0 Answers0