Google Drive PHP客户端库检索文件资源列表 [英] Google Drive PHP Client Library Retrieve a list of File resources
问题描述
我尝试在我的代码中使用此功能: https://开发人员.google.com / drive / v2 / reference / files / list
以下是:
/ **
*检索文件资源列表。
*
* @param apiDriveService $ service Drive API服务实例。
* @return数组文件资源列表。
* /
函数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()); //< ----例外就是扔在那里!
$ pageToken = $ files-> getNextPageToken();
} catch(Exception $ e){
print发生错误:。 $ E->的getMessage();
$ pageToken = NULL;
}
} while($ pageToken);
返回$ result;
}
但是我得到这个错误:
致命错误:调用
中非对象的成员函数getItems()C:\程序文件
(x86)\ EasyPHP- 5.3.6.1 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
数组
(
[kind] => drive#fileList
[etag] =>WtRjAPZWbDA7_fkFjc5ojsEvE7I / lmSsH-kN3I4LpwShGKUKAM7cxbI
[selfLink] => https://www.googleapis.com/drive/ v2 / files
[items] => Array
(
)
)
解决方案PHP客户端库可以以两种方式运行,并返回对象或关联数组,后者是默认值。
文档中的示例假定您需要库返回对象,否则你将不得不取代以下两个调用:
$ $ $ $ $ $ $ $文件> getItems()
$ files-> getNextPageToken()
与使用关联数组的对应调用相反:
$ files ['items']
$ files ['nextPageToken']
更好的是,您可以通过设置
<$ p $来配置库始终返回对象p>$ apiConfig ['use_objects'] = true;
请检查
config.php
档案更多配置选项:
http://code.google.com/p/google-api-php-client/source/browse/trunk/src/config.php
I try to use this function in my code: https://developers.google.com/drive/v2/reference/files/list
Here below:
/** * 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; }
But i got this error:
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
Thee array seems to be empty may be but it shouldn't be:
Array ( [kind] => drive#fileList [etag] => "WtRjAPZWbDA7_fkFjc5ojsEvE7I/lmSsH-kN3I4LpwShGKUKAM7cxbI" [selfLink] => https://www.googleapis.com/drive/v2/files [items] => Array ( ) )
解决方案The PHP client library can operate in two ways and either return objects or associative arrays, with the latter being the default.
The examples in the documentation assume you want the library to return objects, otherwise you would have to replace the following two calls:
$files->getItems() $files->getNextPageToken()
with the corresponding calls that use associative arrays instead:
$files['items'] $files['nextPageToken']
Even better, you can configure the library to always return objects by setting
$apiConfig['use_objects'] = true;
Please check the
config.php
file for more configuration options:http://code.google.com/p/google-api-php-client/source/browse/trunk/src/config.php
这篇关于Google Drive PHP客户端库检索文件资源列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!