16.9. Using the YouTube data API

The YouTube data API offers read-only access to YouTube's content in the form of Google data ("GData") feeds. The API gives access to lists of videos matching specific criteria, popular videos, and more. It also allows access to public information about YouTube users, including playlists, subscriptions and contacts.

For more information on the YouTube data API, please refer to the YouTube data API documentation.

[Замечание] Authentication

The YouTube data API currently allows read-only access to public data, so no authentication is required.

16.9.1. Retrieving 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 reference guide for a detailed list of all feeds. All the feeds described in this section are "video feeds" -- feeds that return lists of videos.

16.9.1.1. Searching for videos by metadata

You can retrieve a list of videos that match specified search criteria, using the video feed. 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.

<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');

$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->mediaGroup->title->text . "\n";
  echo "\nDescription:\n";
  echo $videoEntry->mediaGroup->description->text;
  echo "\n\n\n";
}
?>

For more details on the different query parameters, please refer to the Reference Guide. There are helper functions in Zend_Gdata_YouTube_VideoQuery for each of these parameters.

16.9.1.2. 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:

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

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

16.9.1.3. 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, but many also have helper methods in Zend_Gdata_YouTube.

To retrieve the top rated videos using the helper method:

$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. Only valid with the top_rated and most_viewed standard feeds.

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

$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:

$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery('http://gdata.youtube.com/feeds/standardfeeds/top_rated');
$query->setTime('today');
$videoFeed = $yt->getVideoFeed($query); 

16.9.1.4. 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'.

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

16.9.1.5. 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'.

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

16.9.1.6. 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'.

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

16.9.2. Retrieving video comments

The comments for each YouTube video can be retrieved in several ways. To retrieve the comments for the video with the ID 'abc123813abc', use the following code:

$yt = new Zend_Gdata_YouTube();
$commentFeed = $yt->getVideoCommentFeed('abc123813abc'); 

foreach ($commentFeed as $commentEntry) {
    echo $commentEntry->title->text . "\n";
    echo $commentEntry->content->text . "\n\n\n";
} 

Comments can also be retrieved for a video if you have a copy of the Zend_Gdata_YouTube_VideoEntry object:

$yt = new Zend_Gdata_YouTube();
$videoEntry = $yt->getVideoEntry('abc123813abc'); 
// we don't know the video ID in this example, but we do have the URL
$commentFeed = $yt->getVideoCommentFeed(null, $videoEntry->comments->href);

16.9.3. Retrieving playlist feeds

The YouTube data API provides information about users, including profiles, playlists, subscriptions, and more.

16.9.3.1. Retrieving the playlists of a user

The library provides a helper method to retrieve the playlists associated with a given user. To retrieve the playlists for the user 'liz':

$yt = new Zend_Gdata_YouTube();
$playlistListFeed = $yt->getPlaylistListFeed('liz'); 

foreach ($playlistListFeed as $playlistEntry) {
    echo $playlistEntry->title->text . "\n";
    echo $playlistEntry->description->text . "\n";
    echo $playlistEntry->getPlaylistVideoFeedUrl() . "\n\n\n";
} 

16.9.3.2. Retrieving a specific playlist

The library provides a helper method to retrieve the videos associated with a given playlist. To retrieve the playlists for a specific playlist entry:

$playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistEntry->getPlaylistVideoFeedUrl()); 

16.9.4. Retrieving a list of a user's subscriptions

A user can have several types of subscriptions: channel subscription, tag subscription, or favorites subscription. A Zend_Gdata_YouTube_SubscriptionEntry is used to represent individual subscriptions.

To retrieve all subscriptions for the user 'liz':

$yt = new Zend_Gdata_YouTube();
$subscriptionFeed = $yt->getSubscriptionFeed('liz'); 

foreach ($subscriptionFeed as $subscriptionEntry) {
    echo $subscriptionEntry->title->text . "\n";
} 

16.9.5. Retrieving a user's profile

You can retrieve the public profile information for any YouTube user. To retrieve the profile for the user 'liz':

$yt = new Zend_Gdata_YouTube();
$userProfile = $yt->getUserProfile('liz'); 
echo "username: " . $userProfile->username->text . "\n";
echo "age: " . $userProfile->age->text . "\n";
echo "hometown: " . $userProfile->hometown->text . "\n"; 
    Поддержать сайт на родительском проекте КГБ