Guzzle 6 下载进度

Guzzle 6 progress of download

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

我正在测试以下代码。

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

如有提示,将不胜感激。

虽然文档中没有提及,但您可以使用 "progress" 请求选项。

可以找到对它的引用 here

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