Skip to content

Commit c50232b

Browse files
committed
"Updating samples to reflect recent changes."
1 parent 3e52b14 commit c50232b

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

php/upload_thumbnail.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
// Call set_include_path() as needed to point to your client library.
15-
require_once 'Google_Client.php';
16-
require_once 'contrib/Google_YouTubeService.php';
15+
require_once 'Google/Client.php';
16+
require_once 'Google/Service/YouTube.php';
1717
session_start();
1818

1919
/*
@@ -29,19 +29,20 @@
2929
$client = new Google_Client();
3030
$client->setClientId($OAUTH2_CLIENT_ID);
3131
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
32+
$client->setScopes('https://www.googleapis.com/auth/youtube');
3233
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
3334
FILTER_SANITIZE_URL);
3435
$client->setRedirectUri($redirect);
3536

3637
// Define an object that will be used to make all API requests.
37-
$youtube = new Google_YoutubeService($client);
38+
$youtube = new Google_Service_YouTube($client);
3839

3940
if (isset($_GET['code'])) {
4041
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
4142
die('The session state did not match.');
4243
}
4344

44-
$client->authenticate();
45+
$client->authenticate($_GET['code']);
4546
$_SESSION['token'] = $client->getAccessToken();
4647
header('Location: ' . $redirect);
4748
}
@@ -65,26 +66,41 @@
6566
// value for better recovery on less reliable connections.
6667
$chunkSizeBytes = 1 * 1024 * 1024;
6768

68-
// Create a MediaFileUpload object for resumable uploads.
69-
$media = new Google_MediaFileUpload('image/png', null, true, $chunkSizeBytes);
70-
$media->setFileSize(filesize($imagePath));
69+
// Setting the defer flag to true tells the client to return a request which can be called
70+
// with ->execute(); instead of making the API call immediately.
71+
$client->setDefer(true);
7172

72-
// Call the API's thumbnails.set method to upload the image and associate
73+
// Create a request for the API's thumbnails.set method to upload the image and associate
7374
// it with the appropriate video.
74-
$setResponse = $youtube->thumbnails->set($videoId, array('mediaUpload' => $media));
75+
$setRequest = $youtube->thumbnails->set($videoId);
7576

76-
$uploadStatus = false;
77+
// Create a MediaFileUpload object for resumable uploads.
78+
$media = new Google_Http_MediaFileUpload(
79+
$client,
80+
$setRequest,
81+
'image/png',
82+
null,
83+
true,
84+
$chunkSizeBytes
85+
);
86+
$media->setFileSize(filesize($imagePath));
7787

78-
// Read the image file and upload it chunk by chunk.
88+
89+
// Read the media file and upload it chunk by chunk.
90+
$status = false;
7991
$handle = fopen($imagePath, "rb");
80-
while (!$uploadStatus && !feof($handle)) {
92+
while (!$status && !feof($handle)) {
8193
$chunk = fread($handle, $chunkSizeBytes);
82-
$uploadStatus = $media->nextChunk($setResponse, $chunk);
94+
$status = $media->nextChunk($chunk);
8395
}
8496

8597
fclose($handle);
8698

87-
$thumbnailUrl = $uploadStatus['items'][0]['default']['url'];
99+
// If you want to make other calls after the file upload, set setDefer back to false
100+
$client->setDefer(false);
101+
102+
103+
$thumbnailUrl = $status['items'][0]['default']['url'];
88104
$htmlBody .= "<h3>Thumbnail Uploaded</h3><ul>";
89105
$htmlBody .= sprintf('<li>%s (%s)</li>',
90106
$videoId,

0 commit comments

Comments
 (0)