Zend Framework – Zend_Gdata_Youtube

The Zend Framework contains a number of APIs for accessing Google services, including YouTube. In this article I will show you how to retrieve a feed of videos from YouTube using the Zend_Gdata_YouTube class within Zend_Gdata.

There are a number of different feeds available, and different types of data available in each feed. For instance, some data is only available if you connect to the service with your developer tokens.

To simplify things in this article, we’re going to use the service to retrieve videos for a specific user, and we’re going to do so using anonymous authentication. You can refer to the developer guide (linked below) for details on other types of queries that can be performed.

In this article we will cover the following:

  • Retrieve a list of videos for a given username
  • Loop over the videos and output details of each
  • Use an advanced query to split results up into pages

This article requires Zend Framework, downloadable from http://framework.zend.com. At time of writing, the current version of Zend Framework is 1.10.5.

In addition to this article, you can find out about the Zend_Gdata_YouTube component from Zend Framework developer guide.


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.

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.

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


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.

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.

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.


In our code so far we accessed all videos uploaded by a single user. If the user has a large number of videos this could result in a lot of data. Typically though, you would want to restrict the number of videos returned for each call. To demonstrate this, we’ll split up returned results into pages of 2 videos (you might want a higher limit).

In order to split results into multiple pages, there are several extra steps we must perform:

  1. Specify the maximum number of results to return in the query
  2. Specify the starting point for the query
  3. Determine the total number of results so we know how many pages there will be

In the examples in this section, we’ll pass a URL parameter called p which specifies the current page of results to view. Based on the limit we specify and the page number specified, we must determine the starting point.

Note: With many PHP functions the starting offset begins with 0. When using Zend_Gdata the starting point begins at 1.

Listing 6 shows how to calculate the offset based on the page number and limit.

Now we must update the call to the getUserUploads(). Previously we passed the username as the first argument, but we must now build a more advanced query using Zend_Gdata_Query. We then replace the username with null and pass the query as the second argument.

First we must load the Zend_Gdata_Query class, then instantiate it with the URL from which the fetch the results from.

Note: Unfortunately we must manually build the URI to request data from. Zend_Gdata_YouTube doens’t include a wrapper method to simplify this. I determined the correct URL by looking inside the Zend/Gdata/YouTube.php file.

Next we set the offset and limit using the setStartIndex() and setMaxResults() methods. Listing 7 continues on from the previous listing and shows how to achieve this.

Now you can combine these two listings and call the getUserUploads(). The result is identical to previously and you can loop over the returned feed to access each result.

The final part of splitting results up into pages is to determine the number of pages. To determine the total number of results, call $feed->getTotalResults()->getText();. You then divide this value by the limit to determine the number of pages.

Listing 8 demonstrates how to build and output the pager. Although the output here is a little crude, it demonstrates how to list the pages, and also highlights the active page number.


In this article I have shown you how to access and output a list of videos from YouTube using the Zend_Gdata_YouTube class. I showed you how to embed each video or simply output one or more thumbnails of a video.

Additionally, I showed you how to perform more advanced queries using the Zend_Gdata_Query class. We created a script that splits results up into multiple pages then display links to each page.

There are many other things you can do with this API, including retrieving different types of feeds or even uploading videos directly to YouTube.

Additionally, if you’re going to use the code in this article, I recommend combining if with techniques covered in Zend Framework 101: Zend_Cache. Since the feed data won’t change often, you should reduce the number of calls to the YouTube web service whenever possible as this will result in significant performance gains for your web application.

Further Reading


Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *