Skip to content

Commit 3acd792

Browse files
"Added sample: php/video_localizations.php"
1 parent 00d557b commit 3acd792

1 file changed

Lines changed: 271 additions & 0 deletions

File tree

php/video_localizations.php

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<?php
2+
3+
/**
4+
* This sample sets and retrieves localized metadata for a video by:
5+
*
6+
* 1. Updating language of the default metadata and setting localized metadata
7+
* for a video via "videos.update" method.
8+
* 2. Getting the localized metadata for a video in a selected language using the
9+
* "videos.list" method and setting the "hl" parameter.
10+
* 3. Listing the localized metadata for a video using the "videos.list" method and
11+
* including "localizations" in the "part" parameter.
12+
*
13+
* @author Ibrahim Ulukaya
14+
*/
15+
16+
$htmlBody = <<<END
17+
<form method="GET">
18+
<div>
19+
Action:
20+
<select id="action" name="action">
21+
<option value="set">Set Localization - Fill in: video ID, default language, language, title and description</option>
22+
<option value="get">Get Localization- Fill in: video ID, language</option>
23+
<option value="list">List Localizations - Fill in: video ID, language</option>
24+
</select>
25+
</div>
26+
<br>
27+
<div>
28+
Video ID: <input type="text" id="videoId" name="videoId" placeholder="Enter Video ID">
29+
</div>
30+
<br>
31+
<div>
32+
Default Language: <input type="text" id="defaultLanguage" name="defaultLanguage" placeholder="Enter Default Language (BCP-47 language code)">
33+
</div>
34+
<br>
35+
<div>
36+
Language: <input type="text" id="language" name="language" placeholder="Enter Local Language (BCP-47 language code)">
37+
</div>
38+
<br>
39+
<div>
40+
Title: <input type="text" id="title" name="title" placeholder="Enter Title">
41+
</div>
42+
<br>
43+
<div>
44+
Description: <input type="text" id="description" name="description" placeholder="Enter Description">
45+
</div>
46+
<br>
47+
<input type="submit" value="GO!">
48+
</form>
49+
END;
50+
51+
// Call set_include_path() as needed to point to your client library.
52+
require_once 'Google/Client.php';
53+
require_once 'Google/Service/YouTube.php';
54+
session_start();
55+
56+
57+
/*
58+
* You can acquire an OAuth 2.0 client ID and client secret from the
59+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
60+
* For more information about using OAuth 2.0 to access Google APIs, please see:
61+
* <https://developers.google.com/youtube/v3/guides/authentication>
62+
* Please ensure that you have enabled the YouTube Data API for your project.
63+
*/
64+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
65+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
66+
67+
$action = $_GET['action'];
68+
$videoId = $_GET['videoId'];
69+
$language = $_GET['language'];
70+
$defaultLanguage = $_GET['defaultLanguage'];
71+
$title = $_GET['title'];
72+
$description = $_GET['description'];
73+
74+
$client = new Google_Client();
75+
$client->setClientId($OAUTH2_CLIENT_ID);
76+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
77+
78+
/*
79+
* This OAuth 2.0 access scope allows for full read/write access to the
80+
* authenticated user's account.
81+
*/
82+
$client->setScopes('https://www.googleapis.com/auth/youtube');
83+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
84+
FILTER_SANITIZE_URL);
85+
$client->setRedirectUri($redirect);
86+
87+
// Define an object that will be used to make all API requests.
88+
$youtube = new Google_Service_YouTube($client);
89+
90+
if (isset($_GET['code'])) {
91+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
92+
die('The session state did not match.');
93+
}
94+
95+
$client->authenticate($_GET['code']);
96+
$_SESSION['token'] = $client->getAccessToken();
97+
header('Location: ' . $redirect);
98+
}
99+
100+
if (isset($_SESSION['token'])) {
101+
$client->setAccessToken($_SESSION['token']);
102+
}
103+
104+
// Check to ensure that the access token was successfully acquired.
105+
if ($client->getAccessToken()) {
106+
// This code executes if the user enters an action in the form
107+
// and submits the form. Otherwise, the page displays the form above.
108+
if ($_GET['action']) {
109+
try {
110+
switch ($action) {
111+
case 'set':
112+
setVideoLocalization($youtube, $videoId, $defaultLanguage,
113+
$language, $title, $description, $htmlBody);
114+
break;
115+
case 'get':
116+
getVideoLocalization($youtube, $videoId, $language, $htmlBody);
117+
break;
118+
case 'list':
119+
listVideoLocalizations($youtube, $videoId, $htmlBody);
120+
break;
121+
}
122+
} catch (Google_Service_Exception $e) {
123+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
124+
htmlspecialchars($e->getMessage()));
125+
} catch (Google_Exception $e) {
126+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
127+
htmlspecialchars($e->getMessage()));
128+
}
129+
}
130+
$_SESSION['token'] = $client->getAccessToken();
131+
} else {
132+
// If the user hasn't authorized the app, initiate the OAuth flow
133+
$state = mt_rand();
134+
$client->setState($state);
135+
$_SESSION['state'] = $state;
136+
137+
$authUrl = $client->createAuthUrl();
138+
$htmlBody = <<<END
139+
<h3>Authorization Required</h3>
140+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
141+
END;
142+
}
143+
144+
145+
/**
146+
* Updates a video's default language and sets its localized metadata.
147+
*
148+
* @param Google_Service_YouTube $youtube YouTube service object.
149+
* @param string $videoId The id parameter specifies the video ID for the resource
150+
* that is being updated.
151+
* @param string $defaultLanguage The language of the video's default metadata
152+
* @param string $language The language of the localized metadata
153+
* @param string $title The localized title to be set
154+
* @param string $description The localized description to be set
155+
* @param $htmlBody - html body.
156+
*/
157+
function setVideoLocalization(Google_Service_YouTube $youtube, $videoId, $defaultLanguage,
158+
$language, $title, $description, &$htmlBody) {
159+
// Call the YouTube Data API's videos.list method to retrieve videos.
160+
$videos = $youtube->videos->listVideos("snippet,localizations", array(
161+
'id' => $videoId
162+
));
163+
164+
// If $videos is empty, the specified video was not found.
165+
if (empty($videos)) {
166+
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
167+
} else {
168+
// Since the request specified a video ID, the response only
169+
// contains one video resource.
170+
$updateVideo = $videos[0];
171+
172+
// Modify video's default language and localizations properties.
173+
// Ensure that a value is set for the resource's snippet.defaultLanguage property.
174+
$updateVideo['snippet']['defaultLanguage'] = $defaultLanguage;
175+
$localizations = $updateVideo['localizations'];
176+
177+
if (is_null($localizations)) {
178+
$localizations = array();
179+
}
180+
$localizations[$language] = array('title' => $title, 'description' => $description);
181+
$updateVideo['localizations'] = $localizations;
182+
183+
// Call the YouTube Data API's videos.update method to update an existing video.
184+
$videoUpdateResponse = $youtube->videos->update("snippet,localizations", $updateVideo);
185+
186+
$htmlBody .= "<h2>Updated video</h2><ul>";
187+
$htmlBody .= sprintf('<li>(%s) default language: %s</li>', $videoId,
188+
$videoUpdateResponse['snippet']['defaultLanguage']);
189+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language,
190+
$videoUpdateResponse['localizations'][$language]['title']);
191+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language,
192+
$videoUpdateResponse['localizations'][$language]['description']);
193+
$htmlBody .= '</ul>';
194+
}
195+
}
196+
197+
/**
198+
* Returns localized metadata for a video in a selected language.
199+
* If the localized text is not available in the requested language,
200+
* this method will return text in the default language.
201+
*
202+
* @param Google_Service_YouTube $youtube YouTube service object.
203+
* @param string $videoId The videoId parameter instructs the API to return the
204+
* localized metadata for the video specified by the video id.
205+
* @param string language The language of the localized metadata.
206+
* @param $htmlBody - html body.
207+
*/
208+
function getVideoLocalization(Google_Service_YouTube $youtube, $videoId, $language, &$htmlBody) {
209+
// Call the YouTube Data API's videos.list method to retrieve videos.
210+
$videos = $youtube->videos->listVideos("snippet", array(
211+
'id' => $videoId,
212+
'hl' => $language
213+
));
214+
215+
// If $videos is empty, the specified video was not found.
216+
if (empty($videos)) {
217+
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
218+
} else {
219+
// Since the request specified a video ID, the response only
220+
// contains one video resource.
221+
$localized = $videos[0]["snippet"]["localized"];
222+
223+
$htmlBody .= "<h3>Video</h3><ul>";
224+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localized['title']);
225+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language, $localized['description']);
226+
$htmlBody .= '</ul>';
227+
}
228+
}
229+
230+
/**
231+
* Returns a list of localized metadata for a video.
232+
*
233+
* @param Google_Service_YouTube $youtube YouTube service object.
234+
* @param string $videoId The videoId parameter instructs the API to return the
235+
* localized metadata for the video specified by the video id.
236+
* @param $htmlBody - html body.
237+
*/
238+
function listVideoLocalizations(Google_Service_YouTube $youtube, $videoId, &$htmlBody) {
239+
// Call the YouTube Data API's videos.list method to retrieve videos.
240+
$videos = $youtube->videos->listVideos("snippet,localizations", array(
241+
'id' => $videoId
242+
));
243+
244+
// If $videos is empty, the specified video was not found.
245+
if (empty($videos)) {
246+
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
247+
} else {
248+
// Since the request specified a video ID, the response only
249+
// contains one video resource.
250+
$localizations = $videos[0]["localizations"];
251+
252+
$htmlBody .= "<h3>Video</h3><ul>";
253+
foreach ($localizations as $language => $localization) {
254+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localization['title']);
255+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language, $localization['description']);
256+
}
257+
$htmlBody .= '</ul>';
258+
}
259+
}
260+
261+
?>
262+
263+
<!doctype html>
264+
<html>
265+
<head>
266+
<title>Set and retrieve localized metadata for a video</title>
267+
</head>
268+
<body>
269+
<?=$htmlBody?>
270+
</body>
271+
</html>

0 commit comments

Comments
 (0)