Guzzle 6下载进度
Posted
技术标签:
【中文标题】Guzzle 6下载进度【英文标题】:Guzzle 6 progress of download 【发布时间】:2016-04-27 06:28:01 【问题描述】:我想用 Guzzle 下载一个大文件并想跟踪进度。我不知道我是否必须以某种方式传递流或使用 RequestMediator。
我尝试订阅事件 curl.callback.progress,但 PSR 7 请求没有事件调度程序。 我尝试了on_stats,但回调只在最后触发。 进度订阅者插件已弃用https://github.com/guzzle/progress-subscriber我正在测试以下代码。
$dl = 'http://archive.ubuntu.com/ubuntu/dists/wily/main/installer-amd64/current/images/netboot/mini.iso';
$client = new Client([]);
$request = new GuzzleHttp\Psr7\Request('get', $dl);
$promise = $this->client->sendAsync($request, [
'sink' => '/tmp/test.bin'
]);
$promise->then(function (Response $resp) use ( $fs)
echo 'Finished';
, function (RequestException $e)
);
$promise->wait();
如有提示,将不胜感激。
【问题讨论】:
【参考方案1】:虽然文档中没有提及,但您可以使用“进度”请求选项。
可以在here找到对它的引用。
$options = [
'progress' => function ($dl_total_size, $dl_size_so_far, $ul_total_size, $ul_size_so_far)
// do something.
];
【讨论】:
在我的例子中,这总是返回 0。以上是关于Guzzle 6下载进度的主要内容,如果未能解决你的问题,请参考以下文章