本文介绍了Guzzle 6下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Guzzle下载一个大文件,并想跟踪进度.我不知道我是否必须通过流或以某种方式使用RequestMediator.

I want to download a large file with Guzzle and want to track the progress. I don't know if I have to pass a stream or use the RequestMediator somehow.

  • I tried with subscribing to the event curl.callback.progress, but the PSR 7 Request doesn't have an event dispatcher.
  • I tried the on_stats, but the callback is only fired at the end.
  • The progress subscriber plugin is deprecated 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();

一个提示将不胜感激.

推荐答案

尽管在文档中未提及,但您可以使用进度"请求选项.

Though, its not mentioned within the documentation, you can use the "progress" request option.

对此的引用可以在此处找到.

References to it can be found here.

$options = [
    'progress' => function ($dl_total_size, $dl_size_so_far, $ul_total_size, $ul_size_so_far) {
        // do something.
    }
];

这篇关于Guzzle 6下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:02