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

Jer*_*sia 3 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;
}
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

致命错误:在第115行的C:\ Program Files(x86)\ EasyPHP-5.3.6.1\www\workspace\CPS\class\controller\CtrlGoogleDrive.php中的非对象上调用成员函数getItems()

你的数组似乎是空的可能是但它不应该是:

Array
(
    [kind] => drive#fileList
    [etag] => "WtRjAPZWbDA7_fkFjc5ojsEvE7I/lmSsH-kN3I4LpwShGKUKAM7cxbI"
    [selfLink] => https://www.googleapis.com/drive/v2/files
    [items] => Array
        (
        )

)
Run Code Online (Sandbox Code Playgroud)

Cla*_*ino 9

PHP客户端库可以以两种方式操作,并返回对象或关联数组,后者是默认值.

文档中的示例假定您希望库返回对象,否则您将不得不替换以下两个调用:

$files->getItems()
$files->getNextPageToken()
Run Code Online (Sandbox Code Playgroud)

使用关联数组的相应调用:

$files['items']
$files['nextPageToken']
Run Code Online (Sandbox Code Playgroud)

更好的是,您可以将库配置为始终通过设置返回对象

$apiConfig['use_objects'] = true;
Run Code Online (Sandbox Code Playgroud)

请检查config.php文件以获取更多配置选项:

http://code.google.com/p/google-api-php-client/source/browse/trunk/src/config.php