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

Retrieving a List of Videos

To get started with Zend_Gdata_YouTube all you need to do is include the Zend/Gdata/YouTube.php file, then instantiate the class. No arguments are required when instantiating Zend_Gdata_YouTube.

Note: As mentioned previously, this service allows developers to authenticate in order to access other functionality. While we won't be doing so in this article, if you wanted to, the credentials are specified when instantiating Zend_Gdata_YouTube.

The Zend_Gdata_YouTube class contains a helper method called getUserUploads() which is used to retrieve all videos for the user specified in the first argument. If the username is not valid, an exception is thrown.

Listing 1 shows how to load and instantiate the class, then retrieve all videos for the username ReciteCMS. To compare the results of your code with the real data, view the ReciteCMS channel on YouTube.

Listing 1 Instantiating Zend_Gdata_YouTube and retrieving user videos (listing-1.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
?>

A successful call to getUserUploads() returns an instance of Zend_Gdata_YouTube_VideoFeed. This class implements the Iterator interface, meaning you can loop directly over the object to access each video.

Listing 2 Looping over returned videos (listing-2.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;
    }
 
    foreach ($feed as $video) {
        // output the details of a single video
    }
?>

In the next section I'll show you how to output the details of each video.

In This Article