Friday, January 11, 2013

YouTube Data API via PHP: Displaying and uploading videos

YouTube Data API via PHP: Displaying and uploading videos


Getting Started

The PHP client library is part of the open-source Zend Framework and is available for dowload at framework.zend.com.

Further documentation for using the PHP Client Library with the YouTube API

The complete PHP Developer's Guide is available at code.google.com: http://code.google.com/apis/youtube/developers_guide_php.html
Please note that the Developer's Guide does not document some of the helper methods that are used in the demo application and listed in this article.
Please use the download provided for 'Powered by YouTube' — http://sites.google.com/site/poweredbyyoutube

Requirements

- PHP 5.2+ required extensions: ctype, dom, libxml, openssl*, spl, standard
* openssl is only required if you want to do signed AuthSub authentication
- A developer key (required to write to the API) from http://code.google.com/apis/youtube/dashboard

Installation

- Extract the files to a folder with appropriate permissions and point the include_path to the folder in your php.ini file
- Run the verification script provided at http://sites.google.com/site/poweredbyyoutube

Running the demo application

  1. Copy files from /demo/YouTubeVideoApp to a location on your server.
  2. Make sure that permissions are set correctly and that your server is configured for PHP
  3. Navigate to the index.php page
  The demo application allows you to perform the following unauthenticated requests:

- retrieve various standard feeds
- search for public videos
- retrieve videos from a particular user

When you authenticate to the YouTube API and pass in your developer key, you can perform write requests. The demo application uses the AuthSub authentication mechanism. Once your application is authorized, you can perform the following actions with the demo application:

- upload videos to my account
- delete videos from my account
- add new playlists to my account
- update existing playlist in my account



PHP Client Library shortcuts

Required includes

The Zend/Loader.php class must be included for each .php script. It can then be used to load various YouTube API related classes:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube'); // an example of loading a class

Using the Zend_Gdata_YouTube service object without authentication

All read-only requests can be be performed without authentication:
$yt = new Zend_Gdata_YouTube();
$topRatedFeed = $yt->getTopRatedVideoFeed();

Iterating through Zend_Gdata_YouTube_VideoFeed objects

Most requests will return some type of Feed object. Most of these will be Zend_Gdata_YouTube_VideoFeed's, but there are also _CommentFeed objects, etc.
foreach($topRatedFeed as $videoEntry) {
  print $videoEntry->getVideoTitle() . "\n";
}

Accessing VideoEntry properties

There are helper methods available in the Zend_Gdata_YouTube_VideoEntry class for all of the meta-data. Some of the most commony used ones are:
getVideoDescription(), getFlashPlayerUrl(), getVideoId(), getVideoThumnails()

Uploading Videos

If you have an authenticated Zend_Gdata_YouTube service object, you can upload videos either directly (sending both video and meta-data in one request) or with browser based upload. A direct upload would look like this:
// Assuming that $yt refers to our authenticated service object
$newEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource('myfile.mov');
$filesource->setContentType('video/quicktime');
$filesource->setSlug('myfile.mov');
$newEntry->setMediaSource($filesource);

$newEntry->setVideoTitle('My Test Movie');
$newEntry->setVideoDescription('A test movie');
$newEntry->setVideoCategory('Comedy'); // this must be a valid category
$newEntry->setVideoTags('cars, funny');

// Perform upload. Make sure you add error handling ! 
$updatedEntry = $yt->insertEntry($newEntry,
  'http://uploads.gdata.youtube.com/feeds/users/default/uploads',
  'Zend_Gdata_YouTube_VideoEntry');

No comments:

Post a Comment