Created
August 28, 2011 18:59
-
-
Save ninetwentyfour/1177051 to your computer and use it in GitHub Desktop.
Upload A Video To Ooyala With PHP - blogpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment