Wednesday, October 12, 2011

How to get a User’s Favorite Videos from the YouTube API

Reading YouTube’s API’s with the Zend Framework is extremely simple. So simple, in fact, that most queries can be written in two lines of code. Yes, two! API access really doesn’t get any easier than it is with the Zend Framework.
Suppose you are an avid YouTube user and you’d like to list all of your favorite YouTube videos on your personal website or blog. How do you get started? If you are using the Zend Framework, it’s a piece of cake. Make sure that the file Zend/Gdata/YouTube.php has been included in your application (either through autoloading or explicitly requiring it), and then run the following code:






//Instantiate the YouTube object, which gives access to YouTube API's from PHP
$youtube = new Zend_Gdata_YouTube();
 
//And load up all the favorites for the given username, into an array
$feed = $youtube->getUserFavorites('joeuser');

$feed is then populated with an array of video objects that ‘joeuser’ has favorited. You can then loop through that array and display thumbnails to your users, save it to a database, or do any other number of things with it. Simple, huh?

No comments:

Post a Comment