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. |
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.
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.
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);
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);
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');
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');
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);
The YouTube data API provides information about users, including profiles, playlists, subscriptions, and more.
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"; }
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"; }
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";