Crouching Kitten
Crouching Kitten

Reputation: 1185

How to get the file modification dates with Google Drive

I query the list of files with the following code:

    $service = new \Google_Service_Drive($google->getClient());
    $files = $service->files->listFiles();

Then the following data is returned (this is a print_r dump). All fields, which are supposed to hold dates are empty (They are actually null). For all files. The fields which are set (file name, id, etc.) are correct. Why are the dates missing?

    [17] => Google_Service_Drive_DriveFile Object
        (
            [collection_key:protected] => spaces
            [appProperties] => 
            [capabilitiesType:protected] => Google_Service_Drive_DriveFileCapabilities
            [capabilitiesDataType:protected] => 
            [contentHintsType:protected] => Google_Service_Drive_DriveFileContentHints
            [contentHintsDataType:protected] => 
            [createdTime] => 
            [description] => 
            [explicitlyTrashed] => 
            [fileExtension] => 
            [folderColorRgb] => 
            [fullFileExtension] => 
            [hasAugmentedPermissions] => 
            [hasThumbnail] => 
            [headRevisionId] => 
            [iconLink] => 
            [id] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
            [imageMediaMetadataDataType:protected] => 
            [isAppAuthorized] => 
            [kind] => drive#file
            [lastModifyingUserType:protected] => Google_Service_Drive_User
            [lastModifyingUserDataType:protected] => 
            [md5Checksum] => 
            [mimeType] => application/vnd.google-apps.spreadsheet
            [modifiedByMe] => 
            [modifiedByMeTime] => 
            [modifiedTime] => 
            [name] => Untitled
            [originalFilename] => 
            [ownedByMe] => 
            [ownersType:protected] => Google_Service_Drive_User
            [ownersDataType:protected] => array
            [parents] => 
            [permissionIds] => 
            [permissionsType:protected] => Google_Service_Drive_Permission
            [permissionsDataType:protected] => array
            [properties] => 
            [quotaBytesUsed] => 
            [shared] => 
            [sharedWithMeTime] => 
            [sharingUserType:protected] => Google_Service_Drive_User
            [sharingUserDataType:protected] => 
            [size] => 
            [spaces] => 
            [starred] => 
            [teamDriveId] => 
            [thumbnailLink] => 
            [thumbnailVersion] => 
            [trashed] => 
            [trashedTime] => 
            [trashingUserType:protected] => Google_Service_Drive_User
            [trashingUserDataType:protected] => 
            [version] => 
            [videoMediaMetadataType:protected] => Google_Service_Drive_DriveFileVideoMediaMetadata
            [videoMediaMetadataDataType:protected] => 
            [viewedByMe] => 
            [viewedByMeTime] => 
            [viewersCanCopyContent] => 
            [webContentLink] => 
            [webViewLink] => 
            [writersCanShare] => 
            [internal_gapi_mappings:protected] => Array
                (
                )

            [modelData:protected] => Array
                (
                )

            [processed:protected] => Array
                (
                )

        )

My composer.json has the following line:

    "google/apiclient": "^2.2",

Upvotes: 2

Views: 2139

Answers (1)

Alberto Favaro
Alberto Favaro

Reputation: 1842

Try this:

<?php

$service = new \Google_Service_Drive($google->getClient());
$files = $service->files->listFiles();

foreach($files->getFiles() as $file){
    $createdTime = $file->getCreatedTime();
    $modifyByMeTime = $file->getModifiedByMeTime();
    $modifiedTime = $file->getModifiedTime();
    $sharedWithMeTime = $file->getSharedWithMeTime();
    $trashedTime = $file->getTrashedTime();
    $viewedByMeTime = $file->getViewedByMeTime();
}

?>

All methods can be found here: Class Google_Service_Drive_DriveFile

[Edit]

Try to add this:

...
    $optParams = array(
      'pageSize' => 10,
      'fields' => 'nextPageToken, files(id, name, createdTime)'
    ); 

    $files = $service->files->listFiles($optParams);
...

Upvotes: 4

Related Questions