Google Drive API v3 - 使用PHP下载文件 [英] Google Drive API v3 - downloading files in PHP
问题描述
我正在尝试使用PHP了解Google Drive API v3的下载流程。使用API v2下载文件我:
files-> get()
,数组参数为alt=> media
来获取文件本身而不是元数据。
他们的例子是:
$ fileId ='0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$ content = $ driveService-> files-> get($ fileId,array(
'alt'=>'media'));
我很难理解它是如何工作的,并且通过代码遍历它,但它没有提供更多信息。
当您调用 get()
时,实际进入 $ content
在这个例子中?它是文件的内容(在处理大文件时这似乎很麻烦 - 当然你会失去内存?!)还是某种类型的流引用,我可以调用 fopen
on?我怎么把这个文件保存到磁盘?
文档没有详细说明你做这个API调用会发生什么,它只是说它执行文件下载?
当您使用文档中指定的 alt => media
参数调用 get()
方法时您将获得底层的HTTP响应,这是一个 Guzzle响应对象(显然客户端库使用Guzzle作为底层传输)。
从那里你可以调用任何Guzzle响应方法,比如 $ response-> getStatusCode()
,或者你可以获得实际文件内容的流。
如果他们在某处记录了这些内容,会有帮助!
编辑:这是一个粗略的例子,如果其他人被卡住如何保存文件。
<?php
date_default_timezone_set(Europe / London);
require_once'vendor / autoload.php';
//我正在使用一个服务帐户,为您的帐户类型使用任何Google认证流程。
putenv('GOOGLE_APPLICATION_CREDENTIALS = / path / to / service / account / key.json');
$ client = new Google_Client();
$ client-> addScope(Google_Service_Drive :: DRIVE);
$ client-> useApplicationDefaultCredentials();
$ service = new Google_Service_Drive($ client);
$ fileId =0Bxxxxxxxxxxxxxxxxxxxx; // Google File ID
$ content = $ service-> files-> get($ fileId,array(alt=>media));
//打开输出文件句柄。
$ outHandle = fopen(/ path / to / destination,w +);
//在我们到达EOF之前,一次读取1024个字节并写入输出文件句柄。 ($!$ content-> getBody() - > eof()){
fwrite($ outHandle,$ content-> getBody() - > read(1024 ));
}
//关闭输出文件句柄。
fclose($ outHandle);
echoDone.\\\
?>
I'm trying to understand the download flow for the Google Drive API v3 using PHP. Using the API v2 to download a file I:
- Got the file metadata
- Used the downloadUrl parameter to get a direct link to the file, attached an oAuth token to it and made a GET request to that.
Using API v3 this appears to have been deprecated, and according to the docs you call files->get()
on the Drive Service with an array parameter of "alt" => "media"
to get the file itself rather than the metadata.
And their example was:
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));
I'm having trouble understanding how this works though and have trawled through the code but it didn't give much more info.
When you call get()
, what actually goes into $content
in the example? Is it the contents of the file (in which case this seems troublesome when dealing with large files - surely you'll get out of memory?!) or is it some type of stream reference that I can call fopen
on? How would I save this file to disk?
The documentation doesn't really go into any detail about what happens when you make that API call, it just says it performs a file download?
I figured it out after a bit of experimenting.
When you call the get()
method with the alt=>media
parameter as specified in the docs you get the underlying HTTP response which is a Guzzle response object (as apparently the client library uses Guzzle for it's underlying transport).
From there you can call any Guzzle response method such as $response->getStatusCode()
or you can get a stream of the actual file content.
Would have been helpful if they had documented this somewhere!
EDIT: Here's a rough example if anyone else gets stuck of how to save a file.
<?php
date_default_timezone_set("Europe/London");
require_once 'vendor/autoload.php';
// I'm using a service account, use whatever Google auth flow for your type of account.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Drive($client);
$fileId = "0Bxxxxxxxxxxxxxxxxxxxx"; // Google File ID
$content = $service->files->get($fileId, array("alt" => "media"));
// Open file handle for output.
$outHandle = fopen("/path/to/destination", "w+");
// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.
while (!$content->getBody()->eof()) {
fwrite($outHandle, $content->getBody()->read(1024));
}
// Close output file handle.
fclose($outHandle);
echo "Done.\n"
?>
这篇关于Google Drive API v3 - 使用PHP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!