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

标签 php google-drive-api

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

相关文章:

PHP 连接表显示重复

php - Apache 是否支持 Windows 上的 PHPIniDir?

python - Google Drive API - 列出我尚未访问的共享文件夹中的文件

oauth-2.0 - 如何强制针对特定用户进行 oauth 身份验证

java - 使用 Java API 对 Google Drive 进行身份验证时出现异常

azure - 将文件下载到 Azure VM

php - 像这样设置变量 $config ['business' ]

php - If 语句重定向

php - 调用未定义的方法 Google_Service_Drive_FileList::getItems()

PHP 重定向而不保存在历史记录中