Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions drive/snippets/drive-v2/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"require": {
"google/apiclient": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "9.5.20",
"monolog/monolog": "^1.17"
}
}
41 changes: 41 additions & 0 deletions drive/snippets/drive-v2/src/DriveCreateDrive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START drive_create_drive]
use Google\Client;
use Google\Service\Drive;
use Ramsey\Uuid\Uuid;
function createDrive() {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$driveMetadata = new Drive\DriveFile(array(
'name' => 'Project Resources'));
$requestId = Uuid::uuid4()->toString();
$drive = $driveService->drives->insert($requestId, $driveMetadata, array(
'fields' => 'id'));
printf("Drive ID: %s\n", $drive->id);

return $drive->id;
}
// [END drive_create_drive]
require_once 'vendor/autoload.php';
createDrive();

43 changes: 43 additions & 0 deletions drive/snippets/drive-v2/src/DriveCreateFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START drive_create_folder]
use Google\Client;
use Google\Service\Drive;
function createFolder() {
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$fileMetadata = new Drive\DriveFile(array([
'name' => 'A',
'mimeType' => 'application/vnd.google-apps.folder']));
$file = $driveService->files->create($fileMetadata, array([
'fields' => 'id']));
printf("Folder ID: %s\n", $file->id);
}catch(Exception $e) {
echo "Error Message: ".$e;
}
}
// [END drive_create_folder]

require_once 'vendor/autoload.php';
createFolder();
43 changes: 43 additions & 0 deletions drive/snippets/drive-v2/src/DriveCreateShortcut.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START drive_create_shortcut]
use Google\Client;
use Google\Service\Drive;
function createShortcut()
{
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$fileMetadata = new Drive\DriveFile(array([
'name' => 'Project plan',
'mimeType' => 'application/vnd.google-apps.drive-sdk']));
$file = $driveService->files->create($fileMetadata, array([
'fields' => 'id']));
printf("File ID: %s\n", $file->id);
} catch(Exception $e) {
echo "Error Message: ". $e;
}

}
// [END drive_create_shortcut]
require_once 'vendor/autoload.php';
createShortcut();
43 changes: 43 additions & 0 deletions drive/snippets/drive-v2/src/DriveDownloadFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START drive_download_file]
use Google\Client;
use Google\Service\Drive;
function downloadFile() {
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$response = $driveService->files->get($fileId, array([
'alt' => 'media']));
$content = $response->getBody()->getContents();

return $content;
} catch(Exception $e) {
echo "Error Message: ". $e;
}

}
// [END drive_download_file]
require_once 'vendor/autoload.php';
downloadFile();
43 changes: 43 additions & 0 deletions drive/snippets/drive-v2/src/DriveExportPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START drive_export_pdf]
use Google\Client;
use Google\Service\Drive;
function exportPdf($fileId)
{
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$response = $driveService->files->export($fileId, 'application/pdf', array([
'alt' => 'media']));
$content = $response->getBody()->getContents();

echo $content;
} catch(Exception $e) {
echo "Error Message: ". $e;
}

}
// [END drive_export_pdf]
require_once 'vendor/autoload.php';
exportPdf("1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo");
42 changes: 42 additions & 0 deletions drive/snippets/drive-v2/src/DriveFetchAppdataFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START drive_fetch_appdata_folder]
use Google\Client;
use Google\Service\Drive;
function fetchAppDataFolder() {
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$client->addScope(Drive::DRIVE_APPDATA);
$driveService = new Drive($client);

$file = $driveService->files->get('appDataFolder', array([
'fields' => 'id'
]));
printf("Folder ID: %s\n", $file->id);
} catch(Exception $e) {
echo "Error Message: ". $e;
}

}
// [END drive_fetch_appdata_folder]
require_once 'vendor/autoload.php';
fetchAppDataFolder();
52 changes: 52 additions & 0 deletions drive/snippets/drive-v2/src/DriveFetchChanges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START drive_fetch_changes]
use Google\Client;
use Google\Service\Drive;
function fetchChanges($savedStartPageToken)
{
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
$pageToken = $savedStartPageToken;
while ($pageToken != null) {
$response = $driveService->changes->listChanges($pageToken, array([
'spaces' => 'drive'
]));
foreach ($response->changes as $change) {
// Process change
printf("Change found for file: %s", $change->fileId);
}
if ($response->newStartPageToken != null) {
// Last page, save this token for the next polling interval
$savedStartPageToken = $response->newStartPageToken;
}
$pageToken = $response->nextPageToken;
}

echo $savedStartPageToken;
}
// [END drive_fetch_changes]
require_once 'vendor/autoload.php';
fetchChanges("100");
41 changes: 41 additions & 0 deletions drive/snippets/drive-v2/src/DriveFetchStartPageToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# [START drive_fetch_start_page_token]
# TODO - PHP client currently chokes on fetching start page token
use Google\Client;
use Google\Service\Drive;
function fetchStartPageToken()
{
try {
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$response = $driveService->changes->getStartPageToken();
printf("Start token: %s\n", $response->startPageToken);
return $response->startPageToken;
} catch (Exception $e) {
echo "Error Message: ". $e;
}

}
// [END drive_fetch_start_page_token]
require_once 'vendor/autoload.php';
fetchStartPageToken();
Loading