guzzlehttp/guzzle 配合 symfony/console 命令行中实现文件下载进度条

guzzlehttp/guzzle 配合 symfony/console 命令行中实现文件下载进度条

guzzlehttp/guzzle 配合 symfony/console 命令行中实现文件下载进度条_第1张图片

安装 guzzlehttp/guzzlesymfony/console

$ composer require guzzlehttp/guzzle
$ composer require symfony/console

代码示例

#!/usr/bin/env php
 $saveFilePath,
    'progress' => function ($totalDownload, $downloaded) use ($output, &$progressBar, &$isDownloaded){
        if ($totalDownload > 0 && $downloaded > 0 && null === $progressBar) {
            $progressBar = new ProgressBar($output, $totalDownload);
            $progressBar->setFormat('very_verbose');
            $progressBar->start();
        }
        if (!$isDownloaded && $progressBar && $totalDownload === $downloaded) {
            $progressBar->finish();
            $output->writeln(PHP_EOL);

            return $isDownloaded = true;
        }
        if ($progressBar) {
            $progressBar->setProgress($downloaded);
        }
    },
]);

$client->get($fileDownloadUrl);
$output->writeln('Download completed');

相关链接

原文链接

你可能感兴趣的:(php)