Zend Framework 101: Zend_Gdata_YouTube
Outputting Video Details
When you loop over aZend_Gdata_YouTube_VideoFeed
object, each element is an instance of Zend_Gdata_YouTube_VideoEntry
. There are a number of methods available to access details about the video. Some of the methods available include:
getVideoId()
- the YouTube ID of the videogetVideoTitle()
- the title of the video as it appears in YouTubegetVideoDescription()
- the description of the video as it appears on YouTubegetVideoWatchUrl()
- the URL of the video on YouTubegetVideoDuration()
- the duration of the video in secondsisVideoEmbeddable()
- returnstrue
if the video publisher has allowed third-party sites to embed the videogetVideoTags()
- returns an array of tags associated with the video
Handling Publish and Updated Timestamps
Additionally, you can access when a video was published or updated usinggetPublished()
and getUpdated()
respectively. The return values are not in a timestamp format, but you can convert it to a timestamp using Zend_Date
.
Listing 3 demonstrates how to achieve this. You must retrieve the date as text from the data returned from
getPublished
or getUpdated()
, then pass it to Zend_Date
. The format to import is as is Zend_Date::ISO_8601
.
Listing 3 Converting publish and updated dates to usable timestamps (listing-3.php)
require_once('Zend/Gdata/YouTube.php'); $username = 'ReciteCMS'; $youtube = new Zend_Gdata_YouTube(); try { $feed = $youtube->getUserUploads($username); } catch (Exception $ex) { echo $ex->getMessage(); exit; } // output the feed data here require_once('Zend/Date.php'); foreach ($feed as $video) { $published = new Zend_Date( $video->getPublished()->getText(), Zend_Date::ISO_8601 ); $updated = new Zend_Date( $video->getUpdated()->getText(), Zend_Date::ISO_8601 ); echo sprintf("Published: %s<br />\n", $published); echo sprintf("Updated: %s<br />\n", $updated); }
Tip: You can either use the
Zend_Date
object to output the date, or call getTimestamp()
and pass the result to PHP's date() function.
Outputting Video Details
Next we loop over the results and output the details of each video. In addition to outputting the title, description, publish date, tags, and a link back to YouTube, we're going to check if the video can be embedded. If so, we output the embed code and substitute in the ID of the video.
Note: You can find this code when viewing a video on YouTube. Click the
Embed
button, then simply replace the ID with the ID of the video you want to embed.
Listing 4 Outputting and embedding videos (listing-4.php)
require_once('Zend/Gdata/YouTube.php'); $username = 'ReciteCMS'; $youtube = new Zend_Gdata_YouTube(); try { $feed = $youtube->getUserUploads($username); } catch (Exception $ex) { echo $ex->getMessage(); exit; } // output the feed data here require_once('Zend/Date.php'); foreach ($feed as $video) { $published = new Zend_Date( $video->getPublished()->getText(), Zend_Date::ISO_8601 ); <h1> echo htmlSpecialChars($video->getVideoTitle()) </h1> <p> Published echo $published . Tagged in echo join(', ', $video->getVideoTags()) </p> <p> echo htmlSpecialChars($video->getVideoDescription()) </p> if ($video->isVideoEmbeddable()) { $url = 'http://www.youtube.com/v/' . $video->getVideoId() . '&fs=1'; $w = 640; $h = 480; <object width=" echo $w " height=" echo $h "> <param name="movie" value=" echo htmlSpecialChars($url) "> </param> <param name="allowFullScreen" value="true"></param> <param name="allowscriptaccess" value="always"></param> <embed src=" echo htmlSpecialChars($url) " type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width=" echo $w " height=" echo $h "> </embed> </object> } <p> <a href=" echo htmlSpecialChars($video->getVideoWatchPageUrl()) "> Watch on YouTube </a> </p> }
Showing Thumbnails Instead of Embedding
Typically when you want to output a list of videos, you'll either want to embed the video directly, or show a screenshot of what the video looks like. The feed data provided by YouTube includes a series of thumbnails of each video.To access these thumbnails, call the
getVideoThumbnails()
method. An array is returned, with each element corresponding to a
different thumbnail. Each thumbnail contains the following properties:
time
- The time in the video the thumbnail was taken (in HH:MM:SS.MMM format)height
- The height of the thumbnailwidth
- The width of the thumbnailurl
- The URL of the thumbnail
Listing 5 demonstrates how to access and output thumbnails.
Listing 5 Outputting video thumbnails (listing-5.php)
require_once('Zend/Gdata/YouTube.php'); $username = 'ReciteCMS'; $youtube = new Zend_Gdata_YouTube(); try { $feed = $youtube->getUserUploads($username); } catch (Exception $ex) { echo $ex->getMessage(); exit; } // output the feed data here foreach ($feed as $video) { <h1> echo htmlSpecialChars($video->getVideoTitle()) </h1> foreach ($video->getVideoThumbnails() as $thumbnail) { <img src=" echo htmlSpecialChars($thumbnail['url']) " width=" echo htmlSpecialChars($thumbnail['width']) " height=" echo htmlSpecialChars($thumbnail['height']) " alt=" echo htmlSpecialChars($thumbnail['time']) " /> } }
No comments:
Post a Comment