Skip to content

Commit e19c807

Browse files
"Added sample: php/playlist_localizations.php"
1 parent 52f1cf4 commit e19c807

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed

php/playlist_localizations.php

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
<?php
2+
3+
/**
4+
* This sample sets and retrieves localized metadata for a playlist by:
5+
*
6+
* 1. Updating language of the default metadata and setting localized metadata
7+
* for a playlist via "playlists.update" method.
8+
* 2. Getting the localized metadata for a playlist in a selected language using the
9+
* "playlists.list" method and setting the "hl" parameter.
10+
* 3. Listing the localized metadata for a playlist using the "playlists.list" method
11+
* and 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: playlist ID, default language, language, , title and description</option>
22+
<option value="get">Get Localization- Fill in: playlist ID, language</option>
23+
<option value="list">List Localizations - Fill in: playlist ID, language</option>
24+
</select>
25+
</div>
26+
<br>
27+
<div>
28+
Playlist ID: <input type="text" id="playlistId" name="playlistId" placeholder="Enter Playlist 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+
$resource = $_GET['resource'];
69+
$playlistId = $_GET['playlistId'];
70+
$language = $_GET['language'];
71+
$defaultLanguage = $_GET['defaultLanguage'];
72+
$title = $_GET['title'];
73+
$description = $_GET['description'];
74+
75+
$client = new Google_Client();
76+
$client->setClientId($OAUTH2_CLIENT_ID);
77+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
78+
79+
/*
80+
* This OAuth 2.0 access scope allows for full read/write access to the
81+
* authenticated user's account.
82+
*/
83+
$client->setScopes('https://www.googleapis.com/auth/youtube');
84+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
85+
FILTER_SANITIZE_URL);
86+
$client->setRedirectUri($redirect);
87+
88+
// Define an object that will be used to make all API requests.
89+
$youtube = new Google_Service_YouTube($client);
90+
91+
if (isset($_GET['code'])) {
92+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
93+
die('The session state did not match.');
94+
}
95+
96+
$client->authenticate($_GET['code']);
97+
$_SESSION['token'] = $client->getAccessToken();
98+
header('Location: ' . $redirect);
99+
}
100+
101+
if (isset($_SESSION['token'])) {
102+
$client->setAccessToken($_SESSION['token']);
103+
}
104+
105+
// Check to ensure that the access token was successfully acquired.
106+
if ($client->getAccessToken()) {
107+
// This code executes if the user enters an action in the form
108+
// and submits the form. Otherwise, the page displays the form above.
109+
if ($_GET['action']) {
110+
try {
111+
switch ($action) {
112+
case 'set':
113+
setPlaylistLocalization($youtube, $playlistId, $defaultLanguage,
114+
$language, $title, $description, $htmlBody);
115+
break;
116+
case 'get':
117+
getPlaylistLocalization($youtube, $playlistId, $language, $htmlBody);
118+
break;
119+
case 'list':
120+
listPlaylistLocalizations($youtube, $playlistId, $htmlBody);
121+
break;
122+
}
123+
} catch (Google_Service_Exception $e) {
124+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
125+
htmlspecialchars($e->getMessage()));
126+
} catch (Google_Exception $e) {
127+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
128+
htmlspecialchars($e->getMessage()));
129+
}
130+
}
131+
$_SESSION['token'] = $client->getAccessToken();
132+
} else {
133+
// If the user hasn't authorized the app, initiate the OAuth flow
134+
$state = mt_rand();
135+
$client->setState($state);
136+
$_SESSION['state'] = $state;
137+
138+
$authUrl = $client->createAuthUrl();
139+
$htmlBody = <<<END
140+
<h3>Authorization Required</h3>
141+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
142+
END;
143+
}
144+
145+
146+
/**
147+
* Updates a playlist's default language and sets its localized metadata.
148+
*
149+
* @param Google_Service_YouTube $youtube YouTube service object.
150+
* @param string $playlistId The id parameter specifies the playlist ID for the resource
151+
* that is being updated.
152+
* @param string $defaultLanguage The language of the playlist's default metadata
153+
* @param string $language The language of the localized metadata
154+
* @param string $title The localized title to be set
155+
* @param string $description The localized description to be set
156+
* @param $htmlBody - html body.
157+
*/
158+
function setPlaylistLocalization(Google_Service_YouTube $youtube, $playlistId, $defaultLanguage,
159+
$language, $title, $description, &$htmlBody) {
160+
// Call the YouTube Data API's playlists.list method to retrieve playlists.
161+
$playlists = $youtube->playlists->listPlaylists("snippet,localizations", array(
162+
'id' => $playlistId
163+
));
164+
165+
// If $playlists is empty, the specified playlist was not found.
166+
if (empty($playlists)) {
167+
$htmlBody .= sprintf('<h3>Can\'t find a playlist with playlist id: %s</h3>', $playlistId);
168+
} else {
169+
// Since the request specified a playlist ID, the response only
170+
// contains one playlist resource.
171+
$updatePlaylist = $playlists[0];
172+
173+
// Modify playlist's default language and localizations properties.
174+
// Ensure that a value is set for the resource's snippet.defaultLanguage property.
175+
$updatePlaylist['snippet']['defaultLanguage'] = $defaultLanguage;
176+
$localizations = $updatePlaylist['localizations'];
177+
178+
if (is_null($localizations)) {
179+
$localizations = array();
180+
}
181+
$localizations[$language] = array('title' => $title, 'description' => $description);
182+
$updatePlaylist['localizations'] = $localizations;
183+
184+
// Call the YouTube Data API's playlists.update method to update an existing playlist.
185+
$playlistUpdateResponse = $youtube->playlists->update("snippet,localizations", $updatePlaylist);
186+
187+
$htmlBody .= "<h2>Updated playlist</h2><ul>";
188+
$htmlBody .= sprintf('<li>(%s) default language: %s</li>', $playlistId,
189+
$playlistUpdateResponse['snippet']['defaultLanguage']);
190+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language,
191+
$playlistUpdateResponse['localizations'][$language]['title']);
192+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language,
193+
$playlistUpdateResponse['localizations'][$language]['description']);
194+
$htmlBody .= '</ul>';
195+
}
196+
}
197+
198+
/**
199+
* Returns localized metadata for a playlist in a selected language.
200+
* If the localized text is not available in the requested language,
201+
* this method will return text in the default language.
202+
*
203+
* @param Google_Service_YouTube $youtube YouTube service object.
204+
* @param string $playlistId The videoId parameter instructs the API to return the
205+
* localized metadata for the playlist specified by the playlist id.
206+
* @param string language The language of the localized metadata.
207+
* @param $htmlBody - html body.
208+
*/
209+
function getPlaylistLocalization(Google_Service_YouTube $youtube, $playlistId,
210+
$language, &$htmlBody) {
211+
// Call the YouTube Data API's playlists.list method to retrieve playlists.
212+
$playlists = $youtube->playlists->listPlaylists("snippet", array(
213+
'id' => $playlistId,
214+
'hl' => $language
215+
));
216+
217+
// If $playlists is empty, the specified playlist was not found.
218+
if (empty($playlists)) {
219+
$htmlBody .= sprintf('<h3>Can\'t find a playlist with playlist id: %s</h3>', $playlistId);
220+
} else {
221+
// Since the request specified a playlist ID, the response only
222+
// contains one playlist resource.
223+
$localized = $playlists[0]["snippet"]["localized"];
224+
225+
$htmlBody .= "<h3>Playlist</h3><ul>";
226+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localized['title']);
227+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language, $localized['description']);
228+
$htmlBody .= '</ul>';
229+
}
230+
}
231+
232+
/**
233+
* Returns a list of localized metadata for a playlist.
234+
*
235+
* @param Google_Service_YouTube $youtube YouTube service object.
236+
* @param string $playlistId The videoId parameter instructs the API to return the
237+
* localized metadata for the playlist specified by the playlist id.
238+
* @param $htmlBody - html body.
239+
*/
240+
function listPlaylistLocalizations(Google_Service_YouTube $youtube, $playlistId, &$htmlBody) {
241+
// Call the YouTube Data API's playlists.list method to retrieve playlists.
242+
$playlists = $youtube->playlists->listPlaylists("snippet", array(
243+
'id' => $playlistId
244+
));
245+
246+
// If $playlists is empty, the specified playlist was not found.
247+
if (empty($playlists)) {
248+
$htmlBody .= sprintf('<h3>Can\'t find a playlist with playlist id: %s</h3>', $playlistId);
249+
} else {
250+
// Since the request specified a playlist ID, the response only
251+
// contains one playlist resource.
252+
$localizations = $playlists[0]["localizations"];
253+
254+
$htmlBody .= "<h3>Playlist</h3><ul>";
255+
foreach ($localizations as $language => $localization) {
256+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localization['title']);
257+
$htmlBody .= sprintf('<li>description(%s): %s</li>', $language, $localization['description']);
258+
}
259+
$htmlBody .= '</ul>';
260+
}
261+
}
262+
?>
263+
264+
<!doctype html>
265+
<html>
266+
<head>
267+
<title>Set and retrieve localized metadata for a playlist</title>
268+
</head>
269+
<body>
270+
<?=$htmlBody?>
271+
</body>
272+
</html>

0 commit comments

Comments
 (0)