PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Retrieving public video feeds

The YouTube Data API provides numerous feeds that return a list of videos, such as standard feeds, related videos, video responses, user's uploads, and user's favorites. For example, the user's uploads feed returns all videos uploaded by a specific user. See the YouTube API reference guide for a detailed list of available feeds.

Searching for videos by metadata

You can retrieve a list of videos that match specified search criteria, using the YouTubeQuery class. The following query looks for videos which contain the word "cat" in their metadata, starting with the 10th video and displaying 20 videos per page, ordered by the view count.

Example 455. Searching for videos

<?php
$yt 
= new Zend_Gdata_YouTube();
$query $yt->newVideoQuery();
$query->videoQuery 'cat';
$query->startIndex 10;
$query->maxResults 20;
$query->orderBy 'viewCount';

echo 
$query->queryUrl "\n";
$videoFeed $yt->getVideoFeed($query);

foreach (
$videoFeed as $videoEntry) {
    echo 
"---------VIDEO----------\n";
    echo 
"Title: " $videoEntry->getVideoTitle() . "\n";
    echo 
"\nDescription:\n";
    echo 
$videoEntry->getVideoDescription();
    echo 
"\n\n\n";
}

For more details on the different query parameters, please refer to the Reference Guide. The available helper functions in Zend_Gdata_YouTube_VideoQuery for each of these parameters are described in more detail in the PHP Developer's Guide.

Searching for videos by categories and tags/keywords

Searching for videos in specific categories is done by generating a specially formatted URL. For example, to search for comedy videos which contain the keyword dog:

Example 456. Searching for videos in specific categories

<?php
$yt 
= new Zend_Gdata_YouTube();
$query $yt->newVideoQuery();
$query->category 'Comedy/dog';

echo 
$query->queryUrl "\n";
$videoFeed $yt->getVideoFeed($query);

Retrieving standard feeds

The YouTube Data API has a number of standard feeds. These standard feeds can be retrieved as Zend_Gdata_YouTube_VideoFeed objects using the specified URLs, using the predefined constants within the Zend_Gdata_YouTube class (Zend_Gdata_YouTube::STANDARD_TOP_RATED_URI for example) or using the predefined helper methods (see code listing below).

To retrieve the top rated videos using the helper method:

Example 457. Retrieving a standard video feed

<?php
$yt 
= new Zend_Gdata_YouTube();
$videoFeed $yt->getTopRatedVideoFeed();

There are also query parameters to specify the time period over which the standard feed is computed.

For example, to retrieve the top rated videos for today:

Example 458. Using a Zend_Gdata_YouTube_VideoQuery to Retrieve Videos

<?php
$yt 
= new Zend_Gdata_YouTube();
$query $yt->newVideoQuery();
$query->setTime('today');
$videoFeed $yt->getTopRatedVideoFeed($query);

Alternatively, you could just retrieve the feed using the URL:

Example 459. Retrieving a video feed by URL

<?php
$yt 
= new Zend_Gdata_YouTube();
$url 'http://gdata.youtube.com/feeds/standardfeeds/top_rated?time=today'
$videoFeed $yt->getVideoFeed($url);

Retrieving videos uploaded by a user

You can retrieve a list of videos uploaded by a particular user using a simple helper method. This example retrieves videos uploaded by the user 'liz'.

Example 460. Retrieving videos uploaded by a specific user

<?php
$yt 
= new Zend_Gdata_YouTube();
$videoFeed $yt->getUserUploads('liz');

Retrieving videos favorited by a user

You can retrieve a list of a user's favorite videos using a simple helper method. This example retrieves videos favorited by the user 'liz'.

Example 461. Retrieving a user's favorite videos

<?php
$yt 
= new Zend_Gdata_YouTube();
$videoFeed $yt->getUserFavorites('liz');

Retrieving video responses for a video

You can retrieve a list of a video's video responses using a simple helper method. This example retrieves video response for a video with the ID 'abc123813abc'.

Example 462. Retrieving a feed of video responses

<?php
$yt 
= new Zend_Gdata_YouTube();
$videoFeed $yt->getVideoResponseFeed('abc123813abc');

Zend Framework