Youtube Browser-Based Uploader with PHP

Aug 21 2012

Download Example

RD2 wanted to add something special with our 3rd iteration of building the World Traveler Internship website. We created a very unique feature within the application process. The users could upload their submission video and with a little behind the scenes magic, the video is automatically added to a designated World Traveler Internship YouTube account and added to their bio page on the website.

This is made possible using Youtube’s browser-based uploading API v2.0. The example that the API gives you are good, but not great. I wasn’t able to find examples for exactly what I needed in PHP. So I created my own 2 step process for uploading the user’s file to YouTube.

Before you start, I would suggest downloading the example and looking at that as you read the below code. Below are the steps you will need in order to create your browser-based Youtube uploader:

1. Create your 2 step form


token != '' ) : ?>



2. Get the Youtube Token

Once the form is submitted, you will need to submit the title and description to Youtube. Youtube will return a token that you will then use to upload your video.


get_youtube_token.php code:

                ' . stripslashes( $youtube_video_title ) . '
                ' . stripslashes( $youtube_video_description ) . '

$headers = array( "Authorization: GoogleLogin auth=".$authvalue,
             "GData-Version: 2",
             "X-GData-Key: key=".$key,
             "Content-length: ".strlen( $data ),
             "Content-Type: application/atom+xml; charset=UTF-8" );

$curl = curl_init( "");
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_TIMEOUT, 10 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_POST, 1 );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
curl_setopt( $curl, CURLOPT_REFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $curl, CURLOPT_HEADER, 0 );

$response = simplexml_load_string( curl_exec( $curl ) );
curl_close( $curl );

At this point, we check to make sure we have our YouTube token ( $response ), and then show step 2 of our form.

3. Submit the video using the YouTube token and url.

Once the user submits the video to YouTube, it will return 2 url get variables, the status of the video and the id. If the status is 200, then the video upload was successful.


You can then display a message once the YouTube video is uploaded successfully.


Video Successfully Uploaded!

Video's usually take around 2-3 hours to get accepted by youtube. Please check back soon.

Here is your url to view your video:

That should be it! Please download the example and leave a comment below if this article was useful to you.

Download Example

5 Comments to “Youtube Browser-Based Uploader with PHP”

  1. Colin – Thank you for posting this, it was exactly what I was looking for. I agree that the PHP sample was good but not great. Thanks again!

    By Mike Fritzsche on August 26th, 2012 at 8:30 pm
  2. What do you have to fill in $source?

    By Sharp on August 28th, 2012 at 10:57 am
  3. Sharp, the description the API gives for the $source is ‘a short string that identifies your application for logging purposes’. I have updated the content to reflect this in the code. Thanks for catching this! For more information, visit: this page.

    By Colin Mitchell on August 28th, 2012 at 11:08 am
  4. K, thx. Just one question how do you upload the video private?

    By Sharp on August 28th, 2012 at 12:20 pm
  5. Not sure how to do this without messing around with the code. But I found documentation in the API for uploading private videos.

    By Colin Mitchell on August 28th, 2012 at 1:56 pm

Leave a Reply