YouTube Zend Services - Zend

Videos on demand are now available—not only from your local Blockbuster or video rental store but also from your neighbors, friends, and anyone and everyone who wants their 15 minutes of fame. YouTube has become a powerhouse, at first delivering amateur content to the most recent movie studio content in the form of short trailers and movie exclusives.

YouTube also allows users to comment on each submitted video, embed videos onto other web sites, rate videos, join groups, and receive updates when specific users updated their video collection.

Zend Framework contains a service library that neatly bundles into the Zend_Gdata component: Zend_Gdata_YouTube. This component allows you to retrieve content as well as do the following:

  • Search for videos and sort by most popular
  • Retrieve comments on a video
  • Retrieve public access user data
  • Retrieve user playlists
  • Retrieve user subscriptions
  • Retrieve videos uploaded by specific users
  • Retrieve favorite videos
  • Retrieve playlists for users
  • Upload videos

Getting Started with YouTube Services

For each of the services covered throughout this section, you need to sign up for a developer account or a regular account on the site, so be prepared to sign up for multiple accounts.

To have full access to the YouTube web services, the first account you need to create is a YouTube developer account. To sign up for the account, access the YouTube sign-up form located at http://code.google.com/apis/youtube/dashboard/. Once you sign up for an accou.nt you’ll receive a product name, client ID, and developer key. You need both the developer key along with the client ID to send data to YouTube. However, you’ll just be fetching data, so you won’t actually use them in this chapter.

For starters, you’ll use the test controller. After you explore the new functionality, you’ll add it to the LoudBite application.

The controller at this point contains only one action: testYoutubeAction(), as shown in Listing.

Listing TestWSController.php

The action will return the lip-sync duo Milli Vanilli’s video, “Blame it on the Rain” Behind the scenes, Zend Framework is loading all the necessary files, such as the Zend/ Gdata/ YouTube .php file. This class contains all the operations that allow you to work with YouTube and its web services.

Because you’ll fetch only data, you are not required at this point to use the application ID or secret key. So you can leave the Zend_Gdata_Youtube() constructor empty. The constructor accepts four parameters, however: a Zend_HTTP object, application ID, client ID, and developer key. After the $YouTube object is created, call the getVideoEntry() method, which accepts the video ID and the URI location you want to call. You pass in the video ID, NwrL9MV6jSk, of the video that was located by initially fetching the video directly on Youtube.com and parsing out the ID from the URL:

The portion that is important to you is the highlighted portion of the URL, which is the video ID. After you have the video ID, you pass it into the getVideoEntry() method and fetch a Zend_ Gdata_ YouTube_ VideoEntry object. The object is then placed into a variable that will be used in the view.

Create the view in the /views/scripts/testws/ directory and enter the code as shown in Listing; if the path is not there, create it. Now the first view will simply be a display of video titles.

Listing test-youtube.phtml

Bring up the URL http://localhost/test/test-youtube and take a look at what you just created using only a few pieces of code. You should see a page resembling Figure.

Fetching single video results screenshot

You’ll now continue seeing how to use the libraries for searching, fetching comments, and rating videos.

Search Features

Most of the functionality you’ll probably do with YouTube is retrieve content to populate your own application, so you want to spend a bit of time fetching, ordering, and sorting videos. Searching through the mass collection of videos can be accomplished through the query operations in the Zend_Gdata object. These query operations allow you to search through categories, keywords, or a combination of both; determine how many results you require, and sort the results by different criteria.

The new action in Listing demonstrates keyword searches for a specific video using YouTube’s powerful query functionality.

Listing Keyword Search Example

Start by loading the Zend_Gdata_YouTube class and creating a new query using the newVideoQuery() method. The query allows you to set any number of properties. In this case, set the keywords Marvin Gaye along with the total number of results you want to return. After you create a query, pass the query into the getVideoFeed() method, which returns a Zend_Gdata_YouTube_VideoFeed object. Add the object to the view variables to use in the view.

Copy the code from Listing into your TestWSController.php file; then create a new file in the application/ views/scripts/test folder called test-youtube-keyword.phtml and copy the code in Listing into that file. The view in Listing takes the Zend_Gdata_YouTube_VideoFeed object and loops through each video in the object. Because each video in the object is a Zend_Gdata_YouTube object, you can use the same operations as in Listing.

Listing Keyword Search View

Now pull up the URL http://localhost/test/test-youtube-keyword to see the list of Marvin Gaye videos shown in Figure.

Keyword search results

Video Comments and Ratings

Comments and ratings are very important to YouTube because users can determine which videos are worth watching and users can participate in both criticing any video uploads. In the following example, you’ll retrieve the video ratings along with comments.

Retrieving a video rating does not require you to call another web service; you can use either the Zend_Gdata_Extension_Rating object, which is returned by the video search method, or the getVideoRating() method. getVideoRating()accepts the video ID and returns a key-value array that contains the average rating and the number of people who rated the video.

Using the code created in Listing, fetch the video ratings and display them. The only change that you need to make is to the view (the test-youtube.phtml file).

Listing Displaying Ratings

Because the Zend_Gdata_Video object contains the rating information, you simply need to fetch the rating information by calling the getRating() method. getRating() returns a Zend_Gdata_Extension_Rating object containing the total number of users who rated the video (numRaters), the average rating (average), the highest rating (max), and the lowest rating (min). Refreshing the URL http://localhost/test/test-youtube will load a page that looks like Figure.

Rating information screenshot

You can fetch the comments for a particular video in two ways. You can either use getVideo Comment Feed(), which accepts the ID of the video you want to fetch the comments for, or the object that accompanies the video object(as you did with ratings).

Artist Video List Module

The application you’ve been building now has many more items to it. In this section, you’ll add videos to the artist page. You’ll apply what you’ve learned and create a module that allows you to place up to five music videos or related videos of an artist on the artist page.

You need to modify the controller, ArtistController.php, and create a new view, video-list .phtml, in /application/views/scripts/artist. The bulk of the code is already done when it comes to the controller. Load the TestWSController.php file and copy the test Youtube Keyword Action() method to the artist Controller’s videoListAction(). Then update the code, as shown in Listing.

Listing videoListAction()

Listing contains a small portion of updates. The action now initializes the artist that you want to pull data from and also checks whether the artist parameter is empty. If the user tries to load the page with no artist present, an error will trigger; otherwise, the artist name is used as the keyword to fetch videos from YouTube.

The view has also been created. Copy the code in Listing, change the title in the headTitle() method to Loudbite.com . Artists Videos, and save the content into the video-list.phtml file. Now load the URL you should see something similar to Figure. You can also substitute the keyword b52 for anything you like.

videolis screenshot

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Zend Topics