php - 使用 PHP 和 REST API 密钥从 Google Drive 下载
问题描述
我想通过此脚本强制从 PC 客户端上的 Google Drive 下载文件:
<?php
//Get info about the file from a previous page (its name, type e.g. "rar", and file id which is the $download)
$download = $_GET['download'];
$name = $_GET['name'];
$type = $_GET['type'];
//Get json info about the file on drive, used to get its size in bytes
$json = file_get_contents('https://www.googleapis.com/drive/v2/files/'.$download.'?key=MYAPIKEY');
$obj = json_decode($json);
$size=$obj->fileSize;
//Download the file
$file = 'https://www.googleapis.com/drive/v3/files/'.$download.'?key=MYAPIKEY&alt=media';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$name.'.'.$type.'"');
header('Content-Length: '.$size);
readfile($file);
exit;?>
下载后文件损坏了,有帮助吗?[编辑] 我让它工作,如果其他人想知道如何做到这一点:header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
在标题行的开头添加。
解决方案
推荐阅读
- python - python中的“索引1超出了轴0的范围,大小为1”
- c# - Unity,如何在其他场景中停用/激活组件
- javascript - 将由数组 toString() 生成的字符串转换回 Array
- powershell - 执行get-childitem,但以相反的顺序迭代?
- python - 使用鼠标滚轮缩放热图幅度
- python - 为什么 PySpin.CameraList().GetSize() 返回零设备?
- clang - 如何将表达式的类型作为字符串获取?
- javascript - React 状态不一致地从一个组件传递到下一个组件
- php - 在 ElFinder 中仅正确创建 png 文件的缩略图
- samsung-smart-tv - 多台电视和单个 NAS