首页 > 解决方案 > 使用 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"); 在标题行的开头添加。

标签: phpgoogle-drive-api

解决方案


推荐阅读