Google Drive PHP Client Libray Retrieve a list of File resources


I try to use this function in my code :

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-\www\workspace\CPS\class\controller\CtrlGoogleDrive.php on line 115

Thee array seems to be empty may be but it shouldn't be :

    [kind] => drive#fileList
    [etag] => "WtRjAPZWbDA7_fkFjc5ojsEvE7I/lmSsH-kN3I4LpwShGKUKAM7cxbI"
    [selfLink] =>
    [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:


with the corresponding calls that use associative arrays instead:


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:

This video can help you solving your question :)
By: admin