Upload A Video To Ooyala With PHP
Published: 2010-09-11
EDIT : You can find a better/updated Ooyala class here .
Ooyala is a fairly popular video hosting platform. We have started using them where I work, and they seem to be doing a really good job so far. One of the advantages of Ooyala is their nice API. They have some example code for you to use, but one feature that was lacking was uploading. They also have a widget that does do uploading but it is handled through Flash, something I wanted to avoid.
This example builds on top of their existing PHP code. Here is the final code. (You're really going to want to copy paste this into a real code editor, my code tag makes me really mess with my formatting. Apologies.)
<?php
/**
*Publish video to ooyala
*/
function publish(){// this kicks off the massive publish functions
$labels = $_POST['label'];
$file = $_POST['video_file'];
$dir = $file;
$name = explode("/", $dir );
$name = array_reverse($name );//this get just the name of the file and not
//the whole path for the name and title of the video
$nameforshow = $name[0];
$label = '/'.$labels;
$filesize = filesize($dir);
$expires = time() + 900;
$upload = OrdersController::upload(array('expires' => $expires,
'file_size' => $filesize,
'file_name' => $name[0],
'title' => $name[0]),$dir,$label);
//send this to the upload function
}
function upload($params,$dir,$label){//this passes to the send request function
//for the first time
return OrdersController::send_request('create_video', $params,$dir,
$label,'');
}
function upload_complete($params,$label2){//this gets hit after posting the
//video to ooyala
$label3 = $label2;
$dir ='';
$label = '';
return OrdersController::send_request('upload_complete', $params,
'','',$label3);
//goes back to send request for the second time
}
function assign_label($params){//this starts creating and assigning labels
return OrdersController::send_request2('labels', $params);//this kicks off
//send request 2
}
function send_request($request_type, $params,$dir,$label,$label3){
// first time through this hits the create video url and returns a url
//to post the video to. second time through we hit the upload complete url
$ooyala_pcode = 'PUT YOUR OOYALA PARTNER CODE HERE';
$ooyala_scode = 'PUT YOUR OOYALA SECRET CODE HERE';
if (!array_key_exists('expires', $params)) {
$params['expires'] = time() + 900;
}
$string_to_sign = $ooyala_scode;
$url = 'http://api.ooyala.com/ingestion/'.$request_type.'?pcode='.$ooyala_pcode;
$keys = array_keys($params);
sort($keys);
foreach ($keys as $key) {
$string_to_sign .= $key.'='.$params[$key];
$url .= '&'.rawurlencode($key).'='.rawurlencode($params[$key]);
}
$digest = hash('sha256', $string_to_sign, true);
$signature = ereg_replace('=+$', '', trim(base64_encode($digest)));
$url .= '&signature='.rawurlencode($signature);
if ($request_type == 'create_video') {// if type is create video then
//parse the returned xml for the post url
$xml = simplexml_load_file($url);
foreach($xml->children() as $child){
if($child->getName()=='embedCode'){
$embed_code = $child;
}
if($child->getname()=='urls'){
$upload_url = $child->children();
$file= $dir;
$label2 = $label;
$ch = curl_init($upload_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file'=>"@$file"));
//post the file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
if ($postResult != ''){
$upload_complete = OrdersController::upload_complete(array
('embed_code' => $embed_code),$label2);
//after posting you have to hit the upload complete url
//to get ooyala to process the video.
//so kick this up to the upload complete function
}
}
}
}else{
if ($request_type == 'upload_complete') {
// hit the upload complete url
$ch = curl_init($url);
$postResult2 = curl_exec($ch);
curl_close($ch);
$add_label = OrdersController::assign_label(array(
'embedCodes' => $params['embed_code'], 'labels' => $label3,
'mode'=>'assignLabels'));
// this kicks back up to assign label function to add a label
//to the video
}
}
}
function send_request2($request_type, $params)
// this creates the url to hit to add a label to the last uploaded video
{
$ooyala_pcode = 'PUT YOUR OOYALA PARTNER CODE HERE';
$ooyala_scode = 'PUT YOUR OOYALA SECRET CODE HERE';
//Add an expire time of 15 minutes unless otherwise specified
if (!array_key_exists('expires', $params)) {
$params['expires'] = time() + 900;
}
$string_to_sign = $ooyala_scode;
$url = 'http://api.ooyala.com/partner/'.$request_type.'?pcode='.$ooyala_pcode;
$keys = array_keys($params);
sort($keys);
foreach ($keys as $key) {
$string_to_sign .= $key.'='.$params[$key];
$url .= '&'.rawurlencode($key).'='.rawurlencode($params[$key]);
}
$digest = hash('sha256', $string_to_sign, true);
$signature = ereg_replace('=+$', '', trim(base64_encode($digest)));
$url .= '&signature='.rawurlencode($signature);
$ch = curl_init($url);
$postResult3 = curl_exec($ch);
curl_close($ch);//now the video is done and uploaded to ooyala
}
?>
Essentially, you want to create an upload form, and post the values to the publish() function. From there you should be good to go.
I’m sure there is an easier way to do this, so if you know how to improve this, please let me know in the comments.