【问题标题】:How to check if content of google drive folder has changed?如何检查谷歌驱动器文件夹的内容是否已更改?
【发布时间】:2024-05-23 10:55:02
【问题描述】:

我想对文件夹内容进行缓存,因此如果我能以某种方式在谷歌驱动器中的文件夹更改时获取信息然后刷新缓存,那将是很酷的。有办法吗?最好的 php。

【问题讨论】:

    标签: php google-drive-api drive


    【解决方案1】:

    一段时间后,我想出了这个不错的解决方案:

    function isGoogleDriveFolderContentUpdated($lastTime, $folderId, &$client)
    {
    
        $appsactivityService = new Google_Service_Appsactivity($client);
    
        $optParams = array(
            'source'           => 'drive.google.com',
            'drive.ancestorId' => $folderId,
            'pageSize'         => 1,
        );
    
        $results = $appsactivityService->activities->listActivities($optParams);
    
        if (count($results->getActivities()) == 0) {
    
            return 0;
    
        } else {
            $activities = $results->getActivities();
            $activity = $activities[0];
    
            $event = $activity->getCombinedEvent();
            $activityTime = date(DateTime::RFC3339, $event->getEventTimeMillis() / 1000);
    
            $lastTime = strtotime($lastTime);
            $activityTime = strtotime($activityTime);
    
            if ($activityTime > $lastTime) {
                //folder content changed since last check
                return 1;
            }
    
            return 0;
        }
    
    }
    

    可以这样使用:

    echo isGoogleDriveFolderContentUpdated("2018-11-26T21:03:57+01:00" ,"1A9CXgB44F1khfDAzU4t0R322TWh", $client);
    

    第一个参数是上次检查的日期时间、第二个 google 驱动器文件夹 ID 和 client variable 的最后一个引用。 此外,您的应用程序应具有以下范围:https://www.googleapis.com/auth/activity,否则您将收到错误消息。

    【讨论】:

      最近更新 更多