使用 Google 驱动器 PHP API 获取图像

Getting an image using the Google Drive PHP API

我正在尝试使用 PHP API 检索位于 Google 驱动器中的图像。授权有效,但我似乎无法弄清楚如何获取图像文件以将其放入文件夹中。我还没有在网上找到任何结果。

目前正在使用

$driveService->files->get($fileId),

其中 $driveService 是 Google_Service_Drive class.

然后我尝试使用 webContentLink 来下载文件,但 webContentLink 被设置为空。该图像位于一个文件夹中,该文件夹具有授予服务帐户的编辑权限,该服务帐户用于访问图像。

webContentLink 仅适用于二进制文件。如果您使用的是 file.get,您只需编写获取正文并将其写入文件即可。

$file = $service->files->get($fileId, array('alt' => 'media'));
file_put_contents("hello.pdf",$file->getBody());