Skip to content

Commit 96b6234

Browse files
"Added sample: php/channel_section_localizations.php"
1 parent 361a1ef commit 96b6234

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

php/channel_section_localizations.php

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
<?php
2+
3+
/**
4+
* This sample sets and retrieves localized metadata for a channel section by:
5+
*
6+
* 1. Updating language of the default metadata and setting localized metadata
7+
* for a channel section via "channelSections.update" method.
8+
* 2. Getting the localized metadata for a channel section in a selected language using the
9+
* "channelSections.list" method and setting the "hl" parameter.
10+
* 3. Listing the localized metadata for a channel section using the "channelSections.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: channel section ID, default language, language, title</option>
22+
<option value="get">Get Localization- Fill in: channel section ID, language</option>
23+
<option value="list">List Localizations - Fill in: channel section ID, language</option>
24+
</select>
25+
</div>
26+
<br>
27+
<div>
28+
Channel section ID: <input type="text" id="channelSectionId" name="channelSectionId" placeholder="Enter Channel Section ID">
29+
</div>
30+
<br>
31+
<div>
32+
Default Language: <input type="text" id="defaultLanguage" name="defaultLanguage" placeholder="Enter Default Language">
33+
</div>
34+
<br>
35+
<div>
36+
Default Language: <input type="text" id="defaultLanguage" name="defaultLanguage" placeholder="Enter Default Language (BCP-47 language code)">
37+
</div>
38+
<br>
39+
<div>
40+
Language: <input type="text" id="language" name="language" placeholder="Enter Local Language (BCP-47 language code)">
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+
$channelSectionId = $_GET['channelSectionId'];
66+
$language = $_GET['language'];
67+
$defaultLanguage = $_GET['defaultLanguage'];
68+
$title = $_GET['title'];
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+
setChannelSectionLocalization($youtube, $channelSectionId, $defaultLanguage,
109+
$language, $title, $htmlBody);
110+
break;
111+
case 'get':
112+
getChannelSectionLocalization($youtube, $channelSectionId, $language, $htmlBody);
113+
break;
114+
case 'list':
115+
listChannelSectionLocalizations($youtube, $channelSectionId, $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 section's default language and sets its localized metadata.
143+
*
144+
* @param Google_Service_YouTube $youtube YouTube service object.
145+
* @param string $channelSectionId The id parameter specifies the channel section ID
146+
* for the resource that is being updated.
147+
* @param string $defaultLanguage The language of the channel section's default metadata
148+
* @param string $language The language of the localized metadata
149+
* @param string $title The localized title to be set
150+
* @param $htmlBody - html body.
151+
*/
152+
function setChannelSectionLocalization(Google_Service_YouTube $youtube, $channelSectionId,
153+
$defaultLanguage, $language, $title, &$htmlBody) {
154+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
155+
$channelSections = $youtube->channelSections->listChannelSections("snippet,localizations", array(
156+
'id' => $channelSectionId
157+
));
158+
159+
// If $channelSections is empty, the specified channel section was not found.
160+
if (empty($channelSections)) {
161+
$htmlBody .= sprintf('<h3>Can\'t find a channel section with channel section id: %s</h3>',
162+
$channelSectionId);
163+
} else {
164+
// Since the request specified a channel section ID, the response only
165+
// contains one channel section resource.
166+
$updateChannelSection = $channelSections[0];
167+
168+
// Modify channel section's default language and localizations properties.
169+
// Ensure that a value is set for the resource's snippet.defaultLanguage property.
170+
$updateChannelSection['snippet']['defaultLanguage'] = $defaultLanguage;
171+
$localizations = $updateChannelSection['localizations'];
172+
173+
if (is_null($localizations)) {
174+
$localizations = array();
175+
}
176+
$localizations[$language] = array('title' => $title);
177+
$updateChannelSection['localizations'] = $localizations;
178+
179+
// Call the YouTube Data API's channelSections.update method to update an
180+
// existing channel section.
181+
$channelSectionUpdateResponse = $youtube->channels->update("snippet,localizations",
182+
$updateChannelSection);
183+
184+
$htmlBody .= "<h2>Updated channel section</h2><ul>";
185+
$htmlBody .= sprintf('<li>(%s) default language: %s</li>', $channelSectionId,
186+
$channelSectionUpdateResponse['snippet']['defaultLanguage']);
187+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language,
188+
$channelSectionUpdateResponse['localizations'][$language]['title']);
189+
$htmlBody .= '</ul>';
190+
}
191+
}
192+
193+
/**
194+
* Returns localized metadata for a channel section 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 $channelSectionId The channelSectionId parameter instructs the API to return the
200+
* localized metadata for the channel section specified by the channel section id.
201+
* @param string language The language of the localized metadata.
202+
* @param $htmlBody - html body.
203+
*/
204+
function getChannelSectionLocalization(Google_Service_YouTube $youtube, $channelSectionId,
205+
$language, &$htmlBody) {
206+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
207+
$channelSections = $youtube->channelSections->listChannelSections("snippet", array(
208+
'id' => $channelSectionId,
209+
'hl' => $language
210+
));
211+
212+
// If $channelSections is empty, the specified channel section was not found.
213+
if (empty($channelSections)) {
214+
$htmlBody .= sprintf('<h3>Can\'t find a channel section with channel section id: %s</h3>',
215+
$channelSectionId);
216+
} else {
217+
// Since the request specified a channel section ID, the response only
218+
// contains one channel section resource.
219+
$localized = $channelSections[0]["snippet"]["localized"];
220+
221+
$htmlBody .= "<h3>Channel Section</h3><ul>";
222+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localized['title']);
223+
$htmlBody .= '</ul>';
224+
}
225+
}
226+
227+
/**
228+
* Returns a list of localized metadata for a channel section.
229+
*
230+
* @param Google_Service_YouTube $youtube YouTube service object.
231+
* @param string $channelId The channelSectionId parameter instructs the API to return the
232+
* localized metadata for the channel section specified by the channel section id.
233+
* @param $htmlBody - html body.
234+
*/
235+
function listChannelSectionLocalizations(Google_Service_YouTube $youtube,
236+
$channelSectionId, &$htmlBody) {
237+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
238+
$channelSections = $youtube->channelSections->listChannelSections("snippet", array(
239+
'id' => $channelSectionId
240+
));
241+
242+
// If $channelSections is empty, the specified channel section was not found.
243+
if (empty($channelSections)) {
244+
$htmlBody .= sprintf('<h3>Can\'t find a channel section with channel section id: %s</h3>',
245+
$channelSectionId);
246+
} else {
247+
// Since the request specified a channel section ID, the response only
248+
// contains one channel section resource.
249+
$localizations = $channelSections[0]["localizations"];
250+
251+
$htmlBody .= "<h3>Channel Section</h3><ul>";
252+
foreach ($localizations as $language => $localization) {
253+
$htmlBody .= sprintf('<li>title(%s): %s</li>', $language, $localization['title']);
254+
}
255+
$htmlBody .= '</ul>';
256+
}
257+
}
258+
?>
259+
260+
<!doctype html>
261+
<html>
262+
<head>
263+
<title>Set and retrieve localized metadata for a channel section</title>
264+
</head>
265+
<body>
266+
<?=$htmlBody?>
267+
</body>
268+
</html>

0 commit comments

Comments
 (0)