Skip to content

Commit 52f1cf4

Browse files
"Added sample: php/channel_localizations.php"
1 parent 96b6234 commit 52f1cf4

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed

php/channel_localizations.php

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

0 commit comments

Comments
 (0)