Google Drive PHP Client Library检索文件资源列表
作者:互联网
我尝试在我的代码中使用此函数:https://developers.google.com/drive/v2/reference/files/list
在这下面:
/**
* Retrieve a list of File resources.
*
* @param apiDriveService $service Drive API service instance.
* @return Array List of File resources.
*/
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
array_merge($result, $files->getItems()); // <---- Exception is throw there !
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
但我得到了这个错误:
Fatal error: Call to a member function getItems() on a non-object in
C:\Program Files
(x86)\EasyPHP-5.3.6.1\www\workspace\CPS\class\controller\CtrlGoogleDrive.php
on line 115
你的数组似乎是空的可能是但它不应该是:
Array
(
[kind] => drive#fileList
[etag] => "WtRjAPZWbDA7_fkFjc5ojsEvE7I/lmSsH-kN3I4LpwShGKUKAM7cxbI"
[selfLink] => https://www.googleapis.com/drive/v2/files
[items] => Array
(
)
)
解决方法:
PHP客户端库可以以两种方式操作,并返回对象或关联数组,后者是默认值.
文档中的示例假定您希望库返回对象,否则您将不得不替换以下两个调用:
$files->getItems()
$files->getNextPageToken()
使用关联数组的相应调用:
$files['items']
$files['nextPageToken']
更好的是,您可以将库配置为始终通过设置返回对象
$apiConfig['use_objects'] = true;
请查看config.php文件以获取更多配置选项:
http://code.google.com/p/google-api-php-client/source/browse/trunk/src/config.php
标签:php,google-drive-sdk 来源: https://codeday.me/bug/20190723/1512212.html