谷歌api php客户端-获取文件内容


google-api-php-client - Get File Contents

使用以下代码:

// get the file contents
$file = new Google_Service_Drive_DriveFile();
$fileId = 'The File ID';
try {
  echo 'try part <br>';
  $result3 = $service->files->get($fileId);
  echo 'Contents: ' . $result3->getContent().'<br>';
  echo "Title: " . $result3->getTitle() . '<br>';
  echo "Description: " . $result3->getDescription() . '<br>';
  echo "MIME type: " . $result3->getMimeType() . '<br>';
} catch (Exception $e) {
  echo "An error occurred: " . $e->getMessage();
};

除了这一行以外,一切都正常:

echo 'Contents: ' . $result3->getContent().'<br>';

在API中的drive.php文件中,有一个getContent()函数,但它不起作用。如何从文件中获取内容?

如果您在https://developers.google.com/drive/v2/reference/files/get您将看到Get方法有两种形式。默认获取元数据(例如标题、描述等),而alt=media获取内容。或者,您也可以通过downloadUrl属性获取内容。我不知道PHP库是否公开了alt=media选项(我不喜欢库),但downloadUrl方法在我链接的页面上有一个示例,只需向下滚动并单击"PHP"