Guzzle 6 progress of download

元气小坏坏 提交于 2019-12-28 04:15:07

问题


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

I'm testing the following code.

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

An hint would be appreciated.


回答1:


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.
    }
];


来源:https://stackoverflow.com/questions/34916485/guzzle-6-progress-of-download

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!