Wednesday, October 12, 2011

Retrieving Videos By User with the YouTube API

The Zend Framework makes interacting with YouTube’s API (as well as nearly every other Google service) remarkably easy. In fact, the outstanding support for a wide variety of API’s is one of the Zend Framework’s most powerful features.
To access the videos of a specific user, for example, yourself, you can make a simple call to YouTube to retrieve the videos uploaded by that account. This can be especially useful if you would like to display a feed of your (or anyone’s) newest YouTube videos without having to worry about updating your site whenever a new video is added.
To get videos by user from YouTube, simply create a new YouTube object and run the query, like so:
 
 





//Instantiate YouTube object, which provides methods for interacting with the API
$youtube = new Zend_Gdata_YouTube();
 
//Run the query for users with a specific username
$feed = $youtube->getUserUploads('username');

$feed is now populated with an array of objects corresponding to the videos for that user, like magic! Now you can loop through the videos and do what you want with them, for example, displaying thumbnails and links to users, or saving them to a database.
The YouTube API access in Zend Framework is quite excellent. In addition to answering queries like the above, you can also use it to manipulate and upload your videos programmatically, which will be discussed in later articles.

2 comments:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    html5 converter

    ReplyDelete
  2. Thank you for your explanation. I have been searching for help about the returned value of $youtube->getUserUploads('username'). I wasn't sure if it returned an array or not. Thank you so much! :)

    ReplyDelete