index.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * PHP sample code for the YouTube data API. Utilizes the Zend Framework
  23. * Zend_Gdata component to communicate with the YouTube data API.
  24. *
  25. * Requires the Zend Framework Zend_Gdata component and PHP >= 5.2.11
  26. *
  27. * This sample is run from within a web browser. These files are required:
  28. * index.php - the main logic, which interfaces with the YouTube API
  29. * interface.html - the HTML to represent the web UI
  30. * web_browser.css - the CSS to define the interface style
  31. * web_browser.js - the JavaScript used to provide the video list AJAX interface
  32. *
  33. * NOTE: If using in production, some additional precautions with regards
  34. * to filtering the input data should be used. This code is designed only
  35. * for demonstration purposes.
  36. */
  37. /**
  38. * @see Zend_Loader
  39. */
  40. require_once 'Zend/Loader.php';
  41. /**
  42. * @see Zend_Gdata_YouTube
  43. */
  44. Zend_Loader::loadClass('Zend_Gdata_YouTube');
  45. /**
  46. * Finds the URL for the flash representation of the specified video
  47. *
  48. * @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
  49. * @return string|null The URL or null, if the URL is not found
  50. */
  51. function findFlashUrl($entry)
  52. {
  53. foreach ($entry->mediaGroup->content as $content) {
  54. if ($content->type === 'application/x-shockwave-flash') {
  55. return $content->url;
  56. }
  57. }
  58. return null;
  59. }
  60. /**
  61. * Returns a feed of top rated videos for the specified user
  62. *
  63. * @param string $user The username
  64. * @return Zend_Gdata_YouTube_VideoFeed The feed of top rated videos
  65. */
  66. function getTopRatedVideosByUser($user)
  67. {
  68. $userVideosUrl = 'https://gdata.youtube.com/feeds/users/' .
  69. $user . '/uploads';
  70. $yt = new Zend_Gdata_YouTube();
  71. $ytQuery = $yt->newVideoQuery($userVideosUrl);
  72. // order by the rating of the videos
  73. $ytQuery->setOrderBy('rating');
  74. // retrieve a maximum of 5 videos
  75. $ytQuery->setMaxResults(5);
  76. // retrieve only embeddable videos
  77. $ytQuery->setFormat(5);
  78. return $yt->getVideoFeed($ytQuery);
  79. }
  80. /**
  81. * Returns a feed of videos related to the specified video
  82. *
  83. * @param string $videoId The video
  84. * @return Zend_Gdata_YouTube_VideoFeed The feed of related videos
  85. */
  86. function getRelatedVideos($videoId)
  87. {
  88. $yt = new Zend_Gdata_YouTube();
  89. $ytQuery = $yt->newVideoQuery();
  90. // show videos related to the specified video
  91. $ytQuery->setFeedType('related', $videoId);
  92. // order videos by rating
  93. $ytQuery->setOrderBy('rating');
  94. // retrieve a maximum of 5 videos
  95. $ytQuery->setMaxResults(5);
  96. // retrieve only embeddable videos
  97. $ytQuery->setFormat(5);
  98. return $yt->getVideoFeed($ytQuery);
  99. }
  100. /**
  101. * Echo img tags for the first thumbnail representing each video in the
  102. * specified video feed. Upon clicking the thumbnails, the video should
  103. * be presented.
  104. *
  105. * @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
  106. * @return void
  107. */
  108. function echoThumbnails($feed)
  109. {
  110. foreach ($feed as $entry) {
  111. $videoId = $entry->getVideoId();
  112. echo '<img src="' . $entry->mediaGroup->thumbnail[0]->url . '" ';
  113. echo 'width="80" height="72" onclick="ytvbp.presentVideo(\'' . $videoId . '\')">';
  114. }
  115. }
  116. /**
  117. * Echo the video embed code, related videos and videos owned by the same user
  118. * as the specified videoId.
  119. *
  120. * @param string $videoId The video
  121. * @return void
  122. */
  123. function echoVideoPlayer($videoId)
  124. {
  125. $yt = new Zend_Gdata_YouTube();
  126. $entry = $yt->getVideoEntry($videoId);
  127. $videoTitle = $entry->mediaGroup->title;
  128. $videoUrl = findFlashUrl($entry);
  129. $relatedVideoFeed = getRelatedVideos($entry->getVideoId());
  130. $topRatedFeed = getTopRatedVideosByUser($entry->author[0]->name);
  131. print <<<END
  132. <b>$videoTitle</b><br />
  133. <object width="425" height="350">
  134. <param name="movie" value="${videoUrl}&autoplay=1"></param>
  135. <param name="wmode" value="transparent"></param>
  136. <embed src="${videoUrl}&autoplay=1" type="application/x-shockwave-flash" wmode="transparent"
  137. width=425" height="350"></embed>
  138. </object>
  139. END;
  140. echo '<br />';
  141. echoVideoMetadata($entry);
  142. echo '<br /><b>Related:</b><br />';
  143. echoThumbnails($relatedVideoFeed);
  144. echo '<br /><b>Top rated videos by user:</b><br />';
  145. echoThumbnails($topRatedFeed);
  146. }
  147. /**
  148. * Echo video metadata
  149. *
  150. * @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
  151. * @return void
  152. */
  153. function echoVideoMetadata($entry)
  154. {
  155. $title = $entry->mediaGroup->title;
  156. $description = $entry->mediaGroup->description;
  157. $authorUsername = $entry->author[0]->name;
  158. $authorUrl = 'http://www.youtube.com/profile?user=' . $authorUsername;
  159. $tags = $entry->mediaGroup->keywords;
  160. $duration = $entry->mediaGroup->duration->seconds;
  161. $watchPage = $entry->mediaGroup->player[0]->url;
  162. $viewCount = $entry->statistics->viewCount;
  163. $rating = $entry->rating->average;
  164. $numRaters = $entry->rating->numRaters;
  165. $flashUrl = findFlashUrl($entry);
  166. print <<<END
  167. <b>Title:</b> ${title}<br />
  168. <b>Description:</b> ${description}<br />
  169. <b>Author:</b> <a href="${authorUrl}">${authorUsername}</a><br />
  170. <b>Tags:</b> ${tags}<br />
  171. <b>Duration:</b> ${duration} seconds<br />
  172. <b>View count:</b> ${viewCount}<br />
  173. <b>Rating:</b> ${rating} (${numRaters} ratings)<br />
  174. <b>Flash:</b> <a href="${flashUrl}">${flashUrl}</a><br />
  175. <b>Watch page:</b> <a href="${watchPage}">${watchPage}</a> <br />
  176. END;
  177. }
  178. /**
  179. * Echo the list of videos in the specified feed.
  180. *
  181. * @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
  182. * @return void
  183. */
  184. function echoVideoList($feed)
  185. {
  186. echo '<table class="videoList">';
  187. echo '<tbody width="100%">';
  188. foreach ($feed as $entry) {
  189. $videoId = $entry->getVideoId();
  190. $thumbnailUrl = $entry->mediaGroup->thumbnail[0]->url;
  191. $videoTitle = $entry->mediaGroup->title;
  192. $videoDescription = $entry->mediaGroup->description;
  193. print <<<END
  194. <tr onclick="ytvbp.presentVideo('${videoId}')">
  195. <td width="130"><img src="${thumbnailUrl}" /></td>
  196. <td width="100%">
  197. <a href="#">${videoTitle}</a>
  198. <p class="videoDescription">${videoDescription}</p>
  199. </td>
  200. </tr>
  201. END;
  202. }
  203. echo '</table>';
  204. }
  205. /*
  206. * The main controller logic of the YouTube video browser demonstration app.
  207. */
  208. $queryType = isset($_POST['queryType']) ? $_POST['queryType'] : null;
  209. if ($queryType === null) {
  210. /* display the entire interface */
  211. include 'interface.html';
  212. } else if ($queryType == 'show_video') {
  213. /* display an individual video */
  214. if (array_key_exists('videoId', $_POST)) {
  215. $videoId = $_POST['videoId'];
  216. echoVideoPlayer($videoId);
  217. } else if (array_key_exists('videoId', $_GET)) {
  218. $videoId = $_GET['videoId'];
  219. echoVideoPlayer($videoId);
  220. } else {
  221. echo 'No videoId found.';
  222. exit;
  223. }
  224. } else {
  225. /* display a list of videos */
  226. $searchTerm = $_POST['searchTerm'];
  227. $startIndex = $_POST['startIndex'];
  228. $maxResults = $_POST['maxResults'];
  229. $yt = new Zend_Gdata_YouTube();
  230. $query = $yt->newVideoQuery();
  231. $query->setQuery($searchTerm);
  232. $query->setStartIndex($startIndex);
  233. $query->setMaxResults($maxResults);
  234. /* check for one of the standard feeds, or list from 'all' videos */
  235. switch ($queryType) {
  236. case 'most_viewed':
  237. $query->setFeedType('most viewed');
  238. $query->setTime('this_week');
  239. $feed = $yt->getVideoFeed($query);
  240. break;
  241. case 'most_recent':
  242. $query->setFeedType('most recent');
  243. $feed = $yt->getVideoFeed($query);
  244. break;
  245. case 'recently_featured':
  246. $query->setFeedType('recently featured');
  247. $feed = $yt->getVideoFeed($query);
  248. break;
  249. case 'top_rated':
  250. $query->setFeedType('top rated');
  251. $query->setTime('this_week');
  252. $feed = $yt->getVideoFeed($query);
  253. break;
  254. case 'all':
  255. $feed = $yt->getVideoFeed($query);
  256. break;
  257. default:
  258. echo 'ERROR - unknown queryType - "' . $queryType . '"';
  259. break;
  260. }
  261. echoVideoList($feed);
  262. }