download - Guzzle 6.0 使用请求 header 下载文件

标签 download request-headers guzzle6

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

我查看了文档,但没有任何帮助。

我想出了这个,但它仍然无法下载文件

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',
]);

有人使用请求 header 成功下载了文件吗?

最佳答案

我觉得你很困惑。

'/stream/20' 是您要下载文件的 URL。

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';

关于download - Guzzle 6.0 使用请求 header 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48809203/

相关文章:

PHP - JWT 获取 token 错误的段数

php - Guzzle HTTP - 将授权 header 直接添加到请求中

python - React 和 Python 应用程序的下载文件功能

f# - 如何为 Web 请求指定 header

javascript - 无法访问 Set-cookie 响应 header 和/或 cookie 请求 header

node.js - 两个http请求能走到一起吗?如果可以,nodeJS 服务器如何处理它?

php - Symfony 2 - Guzzle 6.X HTTP |获得 body react

ios - 有条件地使用时间戳下载文件,例如curl -z

android - 在下载文件之前从 URL 解析文件名

asp.net - 下载 Office 文件会导致 aspx 页面在打开文件之前重新加载 3 次