更改内容类型HTTP标头谷歌驱动器文件

时间:2018-01-12 18:42:39

标签: php google-drive-api

我想代理一个文件并使用该标头与php:header("Content-Type: application/octet-stream");

我的代码:

<?php
$id = '0B475ByfcR9n4a1JMVEZxQno2Tmc';

$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download'); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);

$object = json_decode(str_replace(')]}\'', '', $result));

exit(header('Location: '. $object->downloadUrl));
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;)

我该怎么办?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您不能简单地添加标题重定向以及其他标题。当浏览器看到位置标题时,它将移动到新位置并从响应中读取这些标题。您需要直接从谷歌驱动器输出文件(这需要服务器下载文件,并将其转发给客户端。这样的事情):

header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;');
$ch2 = curl_init($object->downloadUrl); 
curl_setopt($ch2, CURLOPT_POSTFIELDS, []);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($ch2);