我找不到任何使用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',
]);
是否有人使用请求标头成功下载了文件?
答案 0 :(得分:3)
我觉得你很困惑。
您有'/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';