Upload API Example (Deprecated)

This section provides a simple example in PHP.

A simple but working PHP example illustrating use of the API by integrating the partner upload widget into a web page. This code is available for download at http://www.ooyala.com/api/uploadWidget.php.

<?php

// You can find your partner and secret code under Account > API
// Settings in Backlot and replace these values.

define('OOYALA_PARTNER_CODE', 'lsNTrbQBqCQbH-VA6ALCshAHLWrV');
define('OOYALA_SECRET_CODE', 'hn-Rw2ZH-YwllUYkklL5Zo_7lWJVkrbShZPb5CD1');

class OoyalaPartnerAPI 
{ 
  static function signed_params($params) 
  { 
    if (!array_key_exists('expires', $params)) { 
      $params['expires'] = time() + 900;  // 15 minutes 
    } 
 
    $string_to_sign = OOYALA_SECRET_CODE; 
    $param_string = 'pcode='.OOYALA_PARTNER_CODE; 
    $keys = array_keys($params); 
    sort($keys); 
 
    foreach ($keys as $key) { 
      $string_to_sign .= $key.'='.$params[$key]; 
      $param_string .= '&'.rawurlencode($key).'='.rawurlencode($params[$key]); 
    } 
      
    $digest = hash('sha256', $string_to_sign, true); 
    $signature = ereg_replace('=+$', '', trim(base64_encode($digest))); 
    return $param_string.'&signature='.rawurlencode($signature); 
  } 
} 
 
// Define any default labels to assign and the dynamic label prefix  
// for any user-selected dynamic labels 
  
$param_string = OoyalaPartnerAPI::signed_params(array( 
  'status' => 'pending', 
  'label[0]' => '/uploader-demo/foo', 
  'label[1]' => '/uploader-demo/bar', 
  'dynamic[0]' => '^/uploader-demo/dynamic/', 
  )); 
?> 
 
<html> 
  <head> 
    <title>Uploader Demo</title> 
  </head> 
   
  <body> 
    <script src="http://www.ooyala.com/partner/uploadButton?width=100&height=20&label=Browse%20Button"></script> 
 
    <script type="text/javascript"> 
      function onFileSelected(file) 
      { 
        document.getElementById('file_name').value = file.name; 
      } 
      function onProgress(event) 
      { 
        document.getElementById('status').innerHTML = (parseInt(event.ratio * 10000) / 100) + '%';  
      } 
      function onUploadComplete() 
      { 
        document.getElementById('status').innerHTML = 'Done!'; 
      } 
      function onUploadError(text) 
      { 
        document.getElementById('status').innerHTML = 'Upload Error: ' + text; 
      } 
      function onEmbedCodeReady(embedCode) 
      { 
        // Not used 
        // document.getElementById('embedCode').innerHTML = embedCode; 
      } 
 
      function onOoyalaUploaderReady()  
      { 
        try 
        { 
          ooyalaUploader.setParameters('<?php print $param_string ?>'); 
        } 
        catch(e) 
        { 
          alert(e); 
        } 
        ooyalaUploader.addEventListener('fileSelected', 'onFileSelected');  
        ooyalaUploader.addEventListener('progress', 'onProgress');  
        ooyalaUploader.addEventListener('complete', 'onUploadComplete');  
        ooyalaUploader.addEventListener('error', 'onUploadError');  
        ooyalaUploader.addEventListener('embedCodeReady', 'onEmbedCodeReady');  
          
        document.getElementById('uploadButton').disabled = false;  
      } 
  
      function startUpload() 
      { 
        try 
        { 
          ooyalaUploader.setTitle(document.getElementById('file_name').value); 
          ooyalaUploader.setDescription(document.getElementById('description').value); 

          if (document.getElementById('dynamic_label').value) 
          { 
            ooyalaUploader.addDynamicLabel('/uploader-demo/dynamic/' + document.getElementById('dynamic_label').value); 
          } 
          var errorText = ooyalaUploader.validate();  

          if (errorText)  
          {  
            alert(errorText);  
            return false;  
          }  
          ooyalaUploader.upload(); 
        }  
        catch(e)  
        {  
          alert(e); 
        } 
        return false; 
      } 
    </script> 
    
    <table border="1" cellspacing="0" cellpadding="1"> 
      <tr> 
        <td>Filename:</td> 
        <td><textarea id="file_name" rows="1" cols="29"></textarea></td>
      </tr> 
      <tr> 
        <td>Description:</td> 
        <td><textarea id="description" rows="5" cols="29"></textarea></td> 
      </tr> 
      <tr> 
        <td>Category:</td> 
        <td> 
          <select id="dynamic_label" name="dynamic_label" /> 
            <option value="">None 
            <option value="Sports">Sports 
            <option value="Drama">Drama 
            <option value="News">News 
            <option value="Action">Action 
          </select> 
        </td> 
      </tr> 
    </table> 
    <button id="uploadButton" disabled onclick="return startUpload();">Upload!</button>  
    <div id="status"></div>  
  <%= partial :"ganalytics" %>
</body> 
</html> 

해당 내용이 도움 되었습니까?