Guzzle 6.0 使用请求头下载文件

Pet*_*fin 3 download request-headers guzzle6

我找不到有关如何使用 Guzzle 6.0 下载远程文件的任何示例。我需要在 GET 请求中传递标头。

我已经查看了根本没有帮助的文档。

我想出了这个,但它仍然没有下载文件

require_once('vendor/autoload.php');

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

$client = new Client();

$response = $client->request('GET', '/stream/20', [
    'headers' => [
        'Authorization: Token token' => 'df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g', 
        'Cache-Control' => 'no-cache', 
        'Content-Type' => 'application/pdf'
    ],
    'sink' => 'https://example.com/path/to/file',
]);
Run Code Online (Sandbox Code Playgroud)

有没有人使用请求头成功下载文件?

AdR*_*ock 5

我想你糊涂了。

您所拥有'/stream/20'的是您要下载文件的网址。

sink部分是您要保存图像的位置。

尝试这个....

require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

$client = new Client();

$resource = fopen('test.pdf', 'w');

$response = $client->request('GET', 'https://example.com/path/to/file', [
    'headers' => [
        'Authorization' => 'Token token=df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g', 
        'Cache-Control' => 'no-cache', 
        'Content-Type' => 'application/pdf'
    ],
    'sink' => $resource,
]);

echo 'downloaded';
Run Code Online (Sandbox Code Playgroud)