PHP Code

PHP: Upload a Video to YouTube using Zend Gdata and YouTube API

I got put on the task to see if there was a way you could automate video uploads to YouTube. I was happy to see that YouTube already had an API set up to preform most functions and Zend had a Gdata Class for their popular framework which tied into most of Google’s API services, including YouTube. It was just a matter of some good example code, trial and error until my first video was posted via PHP.

First head over to Zend and download the Zend Framework. Next go to YouTube and apply for a YouTube API Developer Key. Upload the Zend Framework to your web server and then try the following code.

//debug
#error_reporting(E_ALL);

//include Zend Gdata Libs
require_once("Zend/Gdata/ClientLogin.php");
require_once("Zend/Gdata/HttpClient.php");
require_once("Zend/Gdata/YouTube.php");
require_once("Zend/Gdata/App/MediaFileSource.php");
require_once("Zend/Gdata/App/HttpException.php");
require_once('Zend/Uri/Http.php');

//yt account info
$yt_user = ''; //youtube username or gmail account
$yt_pw = ''; //account password
$yt_source = ''; //name of application (can be anything)

//video path
$video_url = '/full/path/to/video';

//yt dev key
$yt_api_key = ''; //your youtube developer key

//login in to YT
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
							              $username = $yt_user,
							              $password = $yt_pw,
							              $service = 'youtube',
							              $client = null,
							              $source = $yt_source, // a short string identifying your application
							              $loginToken = null,
							              $loginCaptcha = null,
							              $authenticationURL);

$yt = new Zend_Gdata_YouTube($httpClient, $yt_source, NULL, $yt_api_key);

$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
 
$filesource = $yt->newMediaFileSource($video_url);
$filesource->setContentType('video/quicktime'); //make sure to set the proper content type. 
$filesource->setSlug('mytestmovie.mov');
 
$myVideoEntry->setMediaSource($filesource);
 
$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
// Note that category must be a valid YouTube category !
$myVideoEntry->setVideoCategory('Comedy');
 
// Set keywords, note that this must be a comma separated string
// and that each keyword cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');
 
// Upload URI for the currently authenticated user

$uploadUrl = "http://uploads.gdata.youtube.com/feeds/users/$yt_user/uploads";
 
// Try to upload the video, catching a Zend_Gdata_App_HttpException
// if availableor just a regular Zend_Gdata_App_Exception
 
try {
    $newEntry = $yt->insertEntry($myVideoEntry,
                                 $uploadUrl,
                                 'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
    echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
    echo $e->getMessage();
}

//this outputs a ton of garbage. not sure what to do with it yet.
#echo "
" . var_dump($newEntry) . "
";

You will need to enter your YouTube account details, developer key and absolute path to the video you want to upload into the variables provided. When you run the script, if you don’t receive any errors, check your YouTube account and the video should be uploaded. A couple notes:

  • Make sure the include paths for the Zend Framework are correct
  • The path for the video must be on your server. You cannot use a URL
  • If it’s not working turn on the debug line at the top to see if you get any errors

For more information about the kind of things you can do, see the YouTube Developer’s Guide

Continue Reading

  • No Related Posts
  • Hi, I’m having trouble with the sample code.
    I’m getting an internal server error. I’m not sure what’s causing this

    here’s the link to the code: http://205.186.128.101/luckyme/video.php

    • Skye

      Are you still having issues? It looks like your script is uploading now?

      Sorry about not responding sooner. Your comment go tagged as spam.

  • Vj

    Is it possible to upload a video right of the user winhout saving it first to the server?

    • Skye

      Well you could probably upload it straight from the temp location: $_FILES[‘file_upload_name’][‘tmp_name’] to youtube without saving it to your server first. They will still be required to post it to you first.

  • Yeah but any of you want to use this method first read full guid of Zend frame word and documments .

  • Vj: It’s simpler than that! If you read up on the API there is an option to have “browser based upload” instead of “direct upload”; this allows you to upload direct from the user. There are a few differences in the code, as it required different tokens etc – but it’s quite easy to implement!

    http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Browser_based_Upload

    I completely missed out this option when I was reading up on the API and planning a script with this in mind; it was only by luck that I found it when I was looking for something very different! I have a PHP class that I’m hacking up at the moment to act like a wrapper around this functionality, with the hope of implementing a few more of the Youtube API features.

    Skye: Awesome code! One of the simplest and most concise pieces I’ve come across; sure – the documentation is pretty good with google when it comes to APIs (especially when compared to wordpress, whom make it pretty difficult at times with outdated docs and a “wiki-web” full of different paths to get lost in!) but this is so short, simple and effective – yet it also demonstrates the whole process right from authentication. The one thing I didn’t like about the Google documentation was that there was no code demonstrating this, but rather snippets showing each individual step! This certainly allowed me to get my head around it a bit quicker…

  • Craig

    Skye, did you have any luck with the $newEntry array after you’ve uploaded a video? The array seems to be protected and I can’t extract any data – can’t seem to find anything in the Google or Zend docs either.

    • Skye

      Sorry Craig. I haven’t used this piece of code in a while so I’m not too sure.

  • brad

    I integrated this code and replaced the dev key, etc. with my info and I get the following error:
    File to be uploaded at http://www.mysite.com/mytest.mov does not exist or is not readable.

    However, when I visit the above URL, the video does in fact exist and plays in the quicktime player. I’ve changed the permissions to 755 just to test and even that does not work.

    I can’t find an answer to this anywhere, has anyone else an idea of what I’m doing wrong or have you come across this error before?

    Thanks!
    Brad

  • damian

    Awesome worked perfectly, brad try just the filename and put it in the same directory

  • Thank!!! very usefully