View source
<?php
define('ZENCODER_LIBRARY_NAME', "ZencoderPHP");
define('ZENCODER_LIBRARY_VERSION', "1.0");
if (!function_exists('json_encode')) {
require_once 'lib/JSON.php';
$GLOBALS['JSON_OBJECT'] = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
function json_encode($value) {
return $GLOBALS['JSON_OBJECT']
->encode($value);
}
function json_decode($value) {
return $GLOBALS['JSON_OBJECT']
->decode($value);
}
}
class ZencoderJob {
var $new_job_url = "https://app.zencoder.com/api/jobs";
var $new_job_params = array();
var $new_job_json;
var $created = false;
var $errors = array();
var $id;
var $outputs = array();
function ZencoderJob($params, $options = array()) {
if ($options["build"]) {
$this
->update_attributes($params);
return true;
}
$this->new_job_params = $params;
$this->created = $this
->create();
}
function create() {
$request = new ZencoderRequest($this->new_job_url, false, $this->new_job_params);
if ($request->successful) {
$this
->update_attributes($request->results);
return true;
}
else {
$this->errors = array_merge($this->errors, $request->errors);
return false;
}
}
function update_attributes($attributes = array()) {
foreach ($attributes as $attr_name => $attr_value) {
if ($attr_name == "outputs" && is_array($attr_value)) {
$this
->create_outputs($attr_value);
}
elseif (!function_exists($this->{$attr_name})) {
$this->{$attr_name} = $attr_value;
}
}
}
function create_outputs($outputs = array()) {
foreach ($outputs as $output_attrs) {
if ($output_attrs["label"]) {
$this->outputs[$output_attrs["label"]] = new ZencoderOutputFile($output_attrs);
}
else {
$this->outputs[] = new ZencoderOutputFile($output_attrs);
}
}
}
}
class ZencoderOutputFile {
var $id;
var $label;
var $url;
var $state;
var $error_message;
var $error_link;
function ZencoderOutputFile($attributes = array()) {
$this
->update_attributes($attributes);
}
function update_attributes($attributes = array()) {
foreach ($attributes as $attr_name => $attr_value) {
if (!function_exists($this->{$attr_name})) {
$this->{$attr_name} = $attr_value;
}
}
}
}
class ZencoderRequest {
var $successful = false;
var $errors = array();
var $raw_results;
var $results;
function ZencoderRequest($url, $api_key = "", $params = "") {
if ($api_key) {
$url .= "?api_key=" . $api_key;
}
if (is_string($params)) {
$json = trim($params);
}
else {
if (is_array($params)) {
$json = json_encode($params);
}
else {
$json = false;
}
}
$request = new ZencoderCURL($url, $json);
if ($request->connected == false) {
$this->errors[] = $request->error;
return;
}
$status_code = intval($request->status_code);
$this->raw_results = $request->results;
$this->results = json_decode($this->raw_results, true);
if ($status_code >= 200 && $status_code <= 206) {
$this->successful = true;
}
else {
if (is_array($this->results["errors"])) {
foreach ($this->results["errors"] as $error) {
$this->errors[] = $error;
}
}
else {
$this->errors[] = "Unknown Error\n\nHTTP Status Code: " . $request->status_code . "\n" . "Raw Results: \n" . $request->raw_results;
}
}
}
}
class ZencoderCURL {
var $options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
),
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => 1,
);
var $connected;
var $results;
var $status_code;
var $error;
function ZencoderCURL($url, $json, $options = array()) {
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Name: " . ZENCODER_LIBRARY_NAME;
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Version: " . ZENCODER_LIBRARY_VERSION;
if ($json) {
$this->options[CURLOPT_POST] = 1;
$this->options[CURLOPT_POSTFIELDS] = $json;
}
foreach ($options as $option_key => $option) {
$this->options[$option_key] = $option;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($ch, $this->options);
$this->results = curl_exec($ch);
$this->status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
$this->error = 'cURL connection error (' . curl_errno($ch) . '): ' . htmlspecialchars(curl_error($ch)) . ' <a href="http://www.google.com/search?q=' . urlencode("curl error " . curl_error($ch)) . '">Search</a>';
$this->connected = false;
}
else {
$this->connected = true;
}
curl_close($ch);
}
}
class ZencoderOutputNotification {
var $output;
var $job;
function ZencoderOutputNotification($params) {
if ($params["output"]) {
$this->output = new ZencoderOutputFile($params["output"]);
}
if ($params["job"]) {
$this->job = new ZencoderJob($params["job"], array(
"build" => true,
));
}
}
function catch_and_parse() {
$notificiation_data = json_decode(trim(file_get_contents('php://input')), true);
return new ZencoderOutputNotification($notificiation_data);
}
}