PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/php/playlist_localizations.php

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