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
index.php
token != '' ) : ?>
Title:
Description:
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.
index.php
get_youtube_token.php code:
' . stripslashes( $youtube_video_title ) . '
' . stripslashes( $youtube_video_description ) . '
'.$youtube_video_category.'
'.$youtube_video_keywords.'
';
$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( "http://gdata.youtube.com/action/GetUploadToken");
curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
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.
index.php
You can then display a message once the YouTube video is uploaded successfully.
index.php
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:http://www.youtube.com/watch?v=
That should be it! Please download the example and leave a comment below if this article was useful to you.
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!
What do you have to fill in $source?
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.
K, thx. Just one question how do you upload the video private?
Not sure how to do this without messing around with the code. But I found documentation in the API for uploading private videos.