Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载谷歌电子表格谷歌API PHP_Php_Google Api_Google Drive Api_Google Sheets Api_Google Api Php Client - Fatal编程技术网

下载谷歌电子表格谷歌API PHP

下载谷歌电子表格谷歌API PHP,php,google-api,google-drive-api,google-sheets-api,google-api-php-client,Php,Google Api,Google Drive Api,Google Sheets Api,Google Api Php Client,我试图下载一个谷歌电子表格文件,但每次都会出错。我基本上是按照这里写的做的:,但是我得到了:警告:未定义的属性:Google\u Service\u Sheets::$files in当我运行以下代码时: $client = new \Google_Client(); $client->setApplicationName('GoogleSheets'); $client->setScopes([\Google_Service_Drive::DRIVE]); $client->

我试图下载一个谷歌电子表格文件,但每次都会出错。我基本上是按照这里写的做的:,但是我得到了:
警告:未定义的属性:Google\u Service\u Sheets::$files in
当我运行以下代码时:

$client = new \Google_Client();
$client->setApplicationName('GoogleSheets');
$client->setScopes([\Google_Service_Drive::DRIVE]);
$client->setAccessType('offline');
$client->setAuthConfig('credentials.json');
$service = new Google_Service_Sheets($client);

$fileId = mySpreadSheetID;

// Retrieve filename.
$file = $service->files->get($fileId);
$fileName = 'Test_'.time();

// Download a file.
$content = $service->files->get($fileId, array("alt" => "media"));
$handle = fopen("./".$fileName, "w+");
while (!$content->getBody()->eof()) { 
    fwrite($handle, $content->getBody()->read(1024));
}
fclose($handle);

我做错了什么?

只有当谷歌工作表的文件是二进制文件时,你才可以这样做。您还应该创建驱动器服务,而不是图纸服务

$service = new Google_Service_Drive($client);
$response = $service->files->export($createdFile->id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array('alt' => 'media' ));
$content = $response->getBody()->getContents();    
file_put_contents('test.xlsx',$content);

我可以导出到.xlsx文件吗?我需要保留样式,在您给我介绍的示例中,创建的文件似乎是.csv格式的,因此没有样式确定只需更改mime类型,试试看。非常感谢。顺便说一句,还有一个问题,您如何发现您需要使用
“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”