gpt4 book ai didi

php - Google Drive PHP Client Library 检索文件资源列表

转载 作者:可可西里 更新时间:2023-11-01 00:10:04 26 4
gpt4 key购买 nike

我尝试在我的代码中使用这个函数: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

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
(
)

)

最佳答案

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 PHP Client Library 检索文件资源列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com