Zend Framework 101: Zend_Gdata_YouTube
Retrieving a List of Videos
To get started withZend_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
The Zend_Gdata_YouTube
.
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)
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
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)
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 }
No comments:
Post a Comment