Google Drive Drive Client Library PHP Get List of File Resources

I am trying to use this function in my code: https://developers.google.com/drive/v2/reference/files/list

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 getItems () member function for non-object in C: \ Program Files (X86) \ EasyPHP-5.3.6.1 \ WWW \ workstation \ CPS \ class \ controller \ CtrlGoogleDrive.php on line 115

The array array seems empty, but it should not be:

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

The PHP client library can work in two ways and return objects or associative arrays, the latter being the default.

The examples in the documentation assume that you want the library to return objects, otherwise you will have to replace the following two calls:

 $files->getItems() $files->getNextPageToken() 

with corresponding calls that use associative arrays instead:

 $files['items'] $files['nextPageToken'] 

Even better, you can set up the library to always return objects by setting

 $apiConfig['use_objects'] = true; 

Please check the config.php for additional configuration options:

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

+8
source

Source: https://habr.com/ru/post/1209325/


All Articles