Skip to content

Commit 8a1a9e4

Browse files
"Added sample: php/upload_thumbnail.php"
1 parent 7b74de9 commit 8a1a9e4

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

php/upload_thumbnail.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/**
4+
* This sample uploads and sets a custom thumbnail for a video.
5+
*
6+
* 1. It uploads an image using the "Google_MediaFileUpload" class.
7+
* 2. It sets the uploaded image as a custom thumbnail to the video by
8+
* calling the API's "youtube.thumbnails.set" method
9+
*
10+
* @author Ibrahim Ulukaya
11+
*/
12+
13+
14+
// 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';
17+
session_start();
18+
19+
/*
20+
* You can acquire an OAuth 2.0 client ID and client secret from the
21+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
22+
* For more information about using OAuth 2.0 to access Google APIs, please see:
23+
* <https://developers.google.com/youtube/v3/guides/authentication>
24+
* Please ensure that you have enabled the YouTube Data API for your project.
25+
*/
26+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
27+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
28+
29+
$client = new Google_Client();
30+
$client->setClientId($OAUTH2_CLIENT_ID);
31+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
32+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
33+
FILTER_SANITIZE_URL);
34+
$client->setRedirectUri($redirect);
35+
36+
// Define an object that will be used to make all API requests.
37+
$youtube = new Google_YoutubeService($client);
38+
39+
if (isset($_GET['code'])) {
40+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
41+
die('The session state did not match.');
42+
}
43+
44+
$client->authenticate();
45+
$_SESSION['token'] = $client->getAccessToken();
46+
header('Location: ' . $redirect);
47+
}
48+
49+
if (isset($_SESSION['token'])) {
50+
$client->setAccessToken($_SESSION['token']);
51+
}
52+
53+
// Check to ensure that the access token was successfully acquired.
54+
if ($client->getAccessToken()) {
55+
try{
56+
57+
// REPLACE this value with the video ID of the video being updated.
58+
$videoId = "VIDEO_ID";
59+
60+
// REPLACE this value with the path to the image file you are uploading.
61+
$imagePath = "/path/to/file.png";
62+
63+
// Specify the size of each chunk of data, in bytes. Set a higher value for
64+
// reliable connection as fewer chunks lead to faster uploads. Set a lower
65+
// value for better recovery on less reliable connections.
66+
$chunkSizeBytes = 1 * 1024 * 1024;
67+
68+
// Create a MediaFileUpload object for resumable uploads.
69+
$media = new Google_MediaFileUpload('image/png', null, true, $chunkSizeBytes);
70+
$media->setFileSize(filesize($imagePath));
71+
72+
// Call the API's thumbnails.set method to upload the image and associate
73+
// it with the appropriate video.
74+
$setResponse = $youtube->thumbnails->set($videoId, array('mediaUpload' => $media));
75+
76+
$uploadStatus = false;
77+
78+
// Read the image file and upload it chunk by chunk.
79+
$handle = fopen($imagePath, "rb");
80+
while (!$uploadStatus && !feof($handle)) {
81+
$chunk = fread($handle, $chunkSizeBytes);
82+
$uploadStatus = $media->nextChunk($setResponse, $chunk);
83+
}
84+
85+
fclose($handle);
86+
87+
$thumbnailUrl = $uploadStatus['items'][0]['default']['url'];
88+
$htmlBody .= "<h3>Thumbnail Uploaded</h3><ul>";
89+
$htmlBody .= sprintf('<li>%s (%s)</li>',
90+
$videoId,
91+
$thumbnailUrl);
92+
$htmlBody .= sprintf('<img src="%s">', $thumbnailUrl);
93+
$htmlBody .= '</ul>';
94+
95+
96+
} catch (Google_ServiceException $e) {
97+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
98+
htmlspecialchars($e->getMessage()));
99+
} catch (Google_Exception $e) {
100+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
101+
htmlspecialchars($e->getMessage()));
102+
}
103+
104+
$_SESSION['token'] = $client->getAccessToken();
105+
} else {
106+
// If the user hasn't authorized the app, initiate the OAuth flow
107+
$state = mt_rand();
108+
$client->setState($state);
109+
$_SESSION['state'] = $state;
110+
111+
$authUrl = $client->createAuthUrl();
112+
$htmlBody = <<<END
113+
<h3>Authorization Required</h3>
114+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
115+
END;
116+
}
117+
?>
118+
119+
<!doctype html>
120+
<html>
121+
<head>
122+
<title>Claim Uploaded</title>
123+
</head>
124+
<body>
125+
<?=$htmlBody?>
126+
</body>
127+
</html>

0 commit comments

Comments
 (0)