YouTube API for CodeIgniter

Derek Jones edited this page Jul 5, 2012 · 17 revisions
Clone this wiki locally

Category:Contributions::Libraries::YouTube API Category:Libraries::YouTube API Category:Library::YouTube API

[strong]NOTE: I have moved the code for this project to GitHub the url is: https://github.com/jimdoescode/CodeIgniter-YouTube-API-Library Please use this wiki page as a reference for how to implement the library but make sure you get the latest version from GitHub.[/strong]

I have finished up work on the YouTube API library. This will let you make YouTube api calls without having to include all of the Zend Gdata libraries. This library has a single dependency which is on an OAuth signing helper I wrote which can be found at

http://codeigniter.com/wiki/OAuth_Helper/

This library only works with OAuth authentication but a number of calls can be made without any authentication at all. You must pass in a few parameters while loading the library these are as follows:

Your YouTube API key this is given to you when you sign up as a developer with YouTube, and it is required.


$params['apikey'] = $this->config->item('youtube_api_key')

Also your oauth data if you are using authentication. You must provide your consumer key and secret, the signing algorithm you are using and if you already have an authenticated user you must pass in their OAuth access token data as an array containing the oauth_token and (if you are using HMAC signing) the oauth_token_secret. All values of the access_token array must be urlencoded.

I keep my static OAuth data in a config file


$params['oauth']['key'] = $this->config->item('google_consumer_key');
$params['oauth']['secret'] = $this->config->item('google_consumer_secret');
$params['oauth']['algorithm'] = $this->config->item('google_signing_algo');
$params['oauth']['access_token'] = array('oauth_token'=>urlencode($token));

After that we can load our library


$this->load->library('youtube', $params);

Now we have free reign to call any API method we choose. All methods will return a string of XML. It is your job to parse the XML and get out whatever you need. Some methods don't require an authenticated user but other do. Consult the library comments for more details. Here is a breakdown of all the methods that are available:


getVideoEntry($videoId, $fullEntry = false)
getRelatedVideoFeed($videoId)
getVideoResponseFeed($videoId)
getVideoCommentFeed($videoId)
getTopRatedVideoFeed()
getMostViewedVideoFeed()
getRecentlyFeaturedVideoFeed()
getWatchOnMobileVideoFeed()
getPlaylistListFeed($user = 'default')
getSubscriptionFeed($user = 'default')
getContactFeed($user = 'default')
getUserUploads($user = 'default')
getUserFavorites($user = 'default')
getUserProfile($user = 'default')
getActivityForUser($user = 'default')
getFriendActivityForCurrentUser()
getInboxFeedForCurrentUser()
getFormUploadToken($metadata)
addComment($videoId, $comment, $commentId = false)
directUpload($path, $contenttype, $metadata, $user = 'default')

Methods where you define the user (by the users name) only require authentication if the user is defined as 'default' which indicates the currently logged in user. If a method is named get***ForCurrentUser then it requires authentication. Finally the getFormUploadToken method also requires an authenticated user to work.

In addition to requiring an authenticated user getFormUploadToken also requires some XML meta data be passed in. You can find out more about what this should look like at:

http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Sending_a_Browser_Upload_API_Request

I wrote a library to get an OAuth access token from a google service it can be found at:

http://codeigniter.com/wiki/OAuth_for_Google/

EDIT: Updated the library to fix a bug with multi-chunked responses from youtube. Also added an API method to add a comment to a video.

EDIT: Fixed issue where newlines in youtube descriptions would cause the library to stop reading even if more data was available.

EDIT: Updated to support direct uploading. Consult my blog for more info.