我找不到任何有关如何使用 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/