PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend Framework 101: Zend_Gdata_YouTube

Outputting Video Details

When you loop over a Zend_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 video
  • getVideoTitle() - the title of the video as it appears in YouTube
  • getVideoDescription() - the description of the video as it appears on YouTube
  • getVideoWatchUrl() - the URL of the video on YouTube
  • getVideoDuration() - the duration of the video in seconds
  • isVideoEmbeddable() - returns true if the video publisher has allowed third-party sites to embed the video
  • getVideoTags() - 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 using getPublished() 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)
<?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)
<?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><?php echo htmlSpecialChars($video->getVideoTitle()) ?></h1>
 
<p>
    Published <?php echo $published ?>.
    Tagged in <?php echo join(', ', $video->getVideoTags()) ?>
</p>
 
<p>
    <?php echo htmlSpecialChars($video->getVideoDescription()) ?>
</p>
 
<?php
    if ($video->isVideoEmbeddable()) {
        $url = 'http://www.youtube.com/v/' . $video->getVideoId() . '&fs=1';
        $w   = 640;
        $h   = 480;
?>
    <object width="<?php echo $w ?>" height="<?php echo $h ?>">
        <param name="movie"
               value="<?php echo htmlSpecialChars($url) ?>">
        </param>
        <param name="allowFullScreen" value="true"></param>
        <param name="allowscriptaccess" value="always"></param>
        <embed src="<?php echo htmlSpecialChars($url) ?>"
               type="application/x-shockwave-flash"
               allowscriptaccess="always"
               allowfullscreen="true"
               width="<?php echo $w ?>" height="<?php echo $h ?>">
        </embed>
    </object>
<?php
    }
?>
 
<p>
    <a href="<?php echo htmlSpecialChars($video->getVideoWatchPageUrl()) ?>">
        Watch on YouTube
    </a>
</p>
 
<?php
    }
?>

Although this code looks a little messy, hopefully you can understand how it works and update it to suit your own design.

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 thumbnail
  • width - The width of the thumbnail
  • url - The URL of the thumbnail

Note that this array may contain thumbnails of a different size taken at the same time. It is up to you to determine how best to display each thumbnail. If there are no thumbnails an empty array is returned.

Listing 5 demonstrates how to access and output thumbnails.

Listing 5 Outputting video thumbnails (listing-5.php)
<?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><?php echo htmlSpecialChars($video->getVideoTitle()) ?></h1>
 
<?php foreach ($video->getVideoThumbnails() as $thumbnail) { ?>
    <img src="<?php echo htmlSpecialChars($thumbnail['url']) ?>"
         width="<?php echo htmlSpecialChars($thumbnail['width']) ?>"
         height="<?php echo htmlSpecialChars($thumbnail['height']) ?>"
         alt="<?php echo htmlSpecialChars($thumbnail['time']) ?>" />
<?php } ?>
 
<?php
    }
?>

In This Article