1
+ <?php
2
+ /**
3
+ * Copyright 2022 Google Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ // [START drive_fetch_changes]
18
+ use Google \Client ;
19
+ use Google \Service \Drive ;
20
+ # TODO - PHP client currently chokes on fetching start page token
21
+ function fetchChanges ()
22
+ {
23
+ try {
24
+ $ client = new Client ();
25
+ $ client ->useApplicationDefaultCredentials ();
26
+ $ client ->addScope (Drive::DRIVE );
27
+ $ driveService = new Drive ($ client );
28
+ # Begin with our last saved start token for this user or the
29
+ # current token from getStartPageToken()
30
+ $ savedStartPageToken = readLine ("Enter Start Page Token: " );
31
+ $ pageToken = $ savedStartPageToken ;
32
+ while ($ pageToken != null ) {
33
+ $ response = $ driveService ->changes ->listChanges ($ pageToken , array ([
34
+ 'spaces ' => 'drive '
35
+ ]));
36
+ foreach ($ response ->changes as $ change ) {
37
+ // Process change
38
+ printf ("Change found for file: %s " , $ change ->fileId );
39
+ }
40
+ if ($ response ->newStartPageToken != null ) {
41
+ // Last page, save this token for the next polling interval
42
+ $ savedStartPageToken = $ response ->newStartPageToken ;
43
+ }
44
+ $ pageToken = $ response ->nextPageToken ;
45
+ }
46
+ echo $ savedStartPageToken ;
47
+ } catch (Exception $ e ) {
48
+ echo "Error Message: " .$ e ;
49
+ }
50
+
51
+ }
52
+ require_once 'vendor/autoload.php ' ;
53
+ // [END drive_fetch_changes]
54
+ fetchChanges ();
0 commit comments