diff --git a/.gitignore b/.gitignore index f7c5815..042245b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ test.php build_docs.sh .DS_Store - +/nbproject/ diff --git a/README.md b/README.md index 5846df7..a622c4b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,35 @@ Zencoder API PHP Library ========================== +Author: [Nathan Sutton] (nsutton (a) brightcove (.) com) + +Company: [Brightcove/Zencoder](http://www.zencoder.com) + +Version: 2.2.3 + +Date: 2014-07-29 + +Repository: + +To help address problems where users cannot modify php.ini to point cURL to their system CA bundle path, or are using a PHP release before 5.3.7, we have extended this library to allow users to set CURLOPT\_CAPATH and CURLOPT\_CAINFO on the cURL connection used to submit requests. + +```php +$zencoder = new Services_Zencoder($my_api_key, 'v2', 'https://app.zencoder.com', false, $my_curlopt_capath, $my_curlopt_cainfo); +``` + +See also the constructor for `Services_Zencoder` for more information on the available arguments. + +See [the cURL CA bundle extraction page](http://curl.haxx.se/docs/caextract.html) for information on obtaining a CA bundle. We recommend using the HTTPS link to download the CA bundle. + + Author: [Zac Shenker] (zshenker (a) brightcove (.) com) + Company: [Brightcove/Zencoder](http://www.zencoder.com) + Version: 2.2.0 + Date: 2014-07-24 + Repository: The Zencoder CA chain certificate has been removed from the library as the bundled cert will be expiring on July 26 2014, @@ -13,9 +38,13 @@ Please contact us at help@zencoder.com with an issues. Author: [Michael Christopher] (mchristopher (a) brightcove (.) com) + Company: [Zencoder - Online Video Encoder](http://www.zencoder.com) + Version: 2.1.1 + Date: 2012-08-02 + Repository: Parts of this library are based on @@ -29,18 +58,23 @@ For more details on the Zencoder API requirements visit To start working with the library, create a new instance of the Services_Zencoder class, passing your API Key as the 1st parameter. - $zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3'); +```php +$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3'); +``` Once you have created the object, you can use it to interact with the API. For full information, see the Documentation folder, but here is a quick overview of some of the functions that can be called: - $zencoder->accounts->create($array); - $zencoder->jobs->create($array); - $zencoder->jobs->progress($job_id); - $zencoder->inputs->details($input_id); - $zencoder->outputs->details($output_id); - $zencoder->notifications->parseIncoming(); +```php +$zencoder->accounts->create($array); +$zencoder->jobs->create($array); +$zencoder->jobs->progress($job_id); +$zencoder->inputs->details($input_id); +$zencoder->outputs->details($output_id); +$zencoder->notifications->parseIncoming(); +$zencoder->reports->details($report_type, $optional_params); +``` Any errors will throw a Services_Zencoder_Exception. You can call getErrors() on an exception and it will return any errors received from the Zencoder API. @@ -48,119 +82,134 @@ and it will return any errors received from the Zencoder API. ENCODING JOB ------------ + The ZencoderJob object creates an encoding job using [cURL](http://zencoder.com/docs/glossary/curl/) to send [JSON](http://zencoder.com/docs/glossary/json/) formatted parameters to Zencoder's encoding API. ### Step 1 + Visit the [API builder](https://app.zencoder.com/api_builder) in your account, and execute a successful encoding job. ### Step 2 + Copy the successful JSON string, starting with the first curly brace "{", and pass it as the parameters for a new ZencoderJob object. Execute the script on your server to test that it works. #### Example -
-    jobs->create(
+  // New Encoding Job
+  $encoding_job = $zencoder->jobs->create(
+    array(
+      "input" => "s3://bucket-name/file-name.avi",
+      "outputs" => array(
         array(
-          "input" => "s3://bucket-name/file-name.avi",
-          "outputs" => array(
-            array(
-              "label" => "web"
-            )
-          )
+          "label" => "web"
         )
-      );
-
-      // Success if we got here
-      echo "w00t! \n\n";
-      echo "Job ID: ".$encoding_job->id."\n";
-      echo "Output ID: ".$encoding_job->outputs['web']->id."\n";
-      // Store Job/Output IDs to update their status when notified or to check their progress.
-    } catch (Services_Zencoder_Exception $e) {
-      // If were here, an error occured
-      echo "Fail :(\n\n";
-      echo "Errors:\n";
-      foreach ($e->getErrors() as $error) echo $error."\n";
-      echo "Full exception dump:\n\n";
-      print_r($e);
-    }
-
-    echo "\nAll Job Attributes:\n";
-    var_dump($encoding_job);
-
-    ?>
-    
+ ) + ) + ); + + // Success if we got here + echo "w00t! \n\n"; + echo "Job ID: ".$encoding_job->id."\n"; + echo "Output ID: ".$encoding_job->outputs['web']->id."\n"; + // Store Job/Output IDs to update their status when notified or to check their progress. +} catch (Services_Zencoder_Exception $e) { + // If were here, an error occured + echo "Fail :(\n\n"; + echo "Errors:\n"; + foreach ($e->getErrors() as $error) echo $error."\n"; + echo "Full exception dump:\n\n"; + print_r($e); +} + +echo "\nAll Job Attributes:\n"; +var_dump($encoding_job); + +?> +``` ### Step 3 + Modify the above script to meet your needs. + Your [API Request History](https://app.zencoder.com/api_requests) may come in handy. + You can revisit your [API builder](https://app.zencoder.com/api_builder) to add/update parameters of the JSON. You can translate the JSON string into nested associative arrays so that you can dynamically change attributes like "input". The previous JSON example would become: - $encoding_job = $zencoder->jobs->create(array( - "input" => "s3://bucket-name/file-name.avi", - "outputs" => array( - array( - "label" => "web" - ) - ) - )); +```php +$encoding_job = $zencoder->jobs->create(array( + "input" => "s3://bucket-name/file-name.avi", + "outputs" => array( + array( + "label" => "web" + ) + ) +)); +``` NOTIFICATION HANDLING ---------------------- + The ZencoderOutputNotification class is used to capture and parse JSON data sent from Zencoder to your app when an output file has been completed. ### Step 1 + Create a script to receive notifications, and upload it to a location on your server that is publicly accessible. #### Example - notifications->parseIncoming(); +// Initialize the Services_Zencoder class +$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3'); - // Check output/job state - if($notification->job->outputs[0]->state == "finished") { - echo "w00t!\n"; +// Catch notification +$notification = $zencoder->notifications->parseIncoming(); - // If you're encoding to multiple outputs and only care when all of the outputs are finished - // you can check if the entire job is finished. - if($notification->job->state == "finished") { - echo "Dubble w00t!"; - } - } elseif ($notification->job->outputs[0]->state == "cancelled") { - echo "Cancelled!\n"; - } else { - echo "Fail!\n"; - echo $notification->job->outputs[0]->error_message."\n"; - echo $notification->job->outputs[0]->error_link; - } +// Check output/job state +if($notification->job->outputs[0]->state == "finished") { + echo "w00t!\n"; - ?> + // If you're encoding to multiple outputs and only care when all of the outputs are finished + // you can check if the entire job is finished. + if($notification->job->state == "finished") { + echo "Dubble w00t!"; + } +} elseif ($notification->job->outputs[0]->state == "cancelled") { + echo "Cancelled!\n"; +} else { + echo "Fail!\n"; + echo $notification->job->outputs[0]->error_message."\n"; + echo $notification->job->outputs[0]->error_link; +} + +?> +``` ### Step 2 + In the parameters for an encoding job, add the URL for your script to the notifications array of each output you want to be notified for. Then submit the job to test if it works. @@ -168,26 +217,74 @@ Then submit the job to test if it works. #### Example - ... - "outputs" => array( - array( - "label" => "web", - "notifications" => array("http://example.com.com/encoding/notification.php") - ), - array( - "label" => "iPhone", - "notifications" => array("http://example.com.com/encoding/notification.php") - ) - ) - ... +```php +... +"outputs" => array( + array( + "label" => "web", + "notifications" => array("http://example.com.com/encoding/notification.php") + ), + array( + "label" => "iPhone", + "notifications" => array("http://example.com.com/encoding/notification.php") + ) +) +... +``` ### Step 3 + Modify the above script to meet your needs. + Your [notifications page](https://app.zencoder.com/notifications) will come in handy. +REPORTS +---------------------- +The ZencoderReports class is used to get reports over the zencoder api. +See [reports api doc](https://app.zencoder.com/docs/api/reports) for required/optional parameters. + +### Get usage for ALL reports +Create a script to get reports for a specified date range + +#### Example +```php + + // Make sure this points to a copy of Zencoder.php on the same server as this script. + require_once('Services/Zencoder.php'); + + // Initialize the Services_Zencoder class + $zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3'); + + // Get reports + $params = array( + 'from' => '2014-02-01', + 'to' => '2014-02-28', + ) + + // 'all' can be replaced by 'vod' or 'live' acccording to entry points in docs + $report = $zencoder->reports->details('all', $params); + + // Each reports object should have a 'statistics' and 'total' base element + if ($report->statistics) { + foreach ($report->statistics as $statistic) { + print_r($statistic); + } + print_r($report->total); + } else { + echo "no statistics found"; + } + +``` + + VERSIONS --------- + + Version 2.2.3 - 2014-07-29 Fixed the versions listed in the user agent and throughout the code + Version 2.2.2 - 2014-07-29 Fixed a bug where api_key was set as api_version in the http connection options + Version 2.2.1 - 2014-07-29 Support setting CURLOPT_CAPATH and CURLOPT_CAINFO on cURL connections. + Version 2.2.0 - 2014-07-24 Removing the bundled CA chain to address expiring intermediate certificate Version 2.1.1 - 2012-08-02 Fixing issue where jobs index call didn't return jobs as individual objects Version 2.1.0 - 2012-06-05 Adding support for job-level notifications & merging output with job in notification object Version 2.0.2 - 2012-01-11 Fixed job creation response object, added documentation to variables diff --git a/Services/Zencoder.php b/Services/Zencoder.php index 6d453d7..050bd64 100644 --- a/Services/Zencoder.php +++ b/Services/Zencoder.php @@ -5,7 +5,7 @@ * @category Services * @package Services_Zencoder * @author Michael Christopher - * @version Release: 2.2.0 + * @version Release: 2.2.3 * @license http://creativecommons.org/licenses/MIT/MIT * @link http://github.com/zencoder/zencoder-php * @access private @@ -26,14 +26,14 @@ function Services_Zencoder_autoload($className) * @category Services * @package Services_Zencoder * @author Michael Christopher - * @version Release: 2.2.0 + * @version Release: 2.2.3 * @license http://creativecommons.org/licenses/MIT/MIT * @link http://github.com/zencoder/zencoder-php */ class Services_Zencoder extends Services_Zencoder_Base { - const USER_AGENT = 'ZencoderPHP v2.2.0'; + const USER_AGENT = 'ZencoderPHP v2.2.3'; /** * Contains the HTTP communication class @@ -88,6 +88,14 @@ class Services_Zencoder extends Services_Zencoder_Base * @var Services_Zencoder_Outputs */ public $outputs; + /** + * Provides access to the Zencoder Reports API + * + * Valid functions: vod, live, minutes, all + * + * @var Services_Zencoder_Reports + */ + public $reports; /** * Initialize the Services_Zencoder class and sub-classes. @@ -96,12 +104,16 @@ class Services_Zencoder extends Services_Zencoder_Base * @param string $api_version API version * @param string $api_host API host * @param bool $debug Enable debug mode + * @param string $ca_path Path to a directory that holds multiple CA certificates + * @param string $ca_file Path to a file holding one or more certificates to verify the peer with */ public function __construct( $api_key = NULL, $api_version = 'v2', $api_host = 'https://app.zencoder.com', - $debug = false + $debug = false, + $ca_path = NULL, + $ca_file = NULL ) { // Check that library dependencies are met @@ -114,18 +126,24 @@ public function __construct( if (!function_exists('curl_init')) { throw new Services_Zencoder_Exception('cURL extension must be enabled.'); } + $this->version = $api_version; - $this->http = new Services_Zencoder_Http( - $api_host, - array("curlopts" => array( - CURLOPT_USERAGENT => self::USER_AGENT - ), "api_key" => $api_key, "debug" => $debug) - ); + + $http_options = array("api_key" => $api_key, "debug" => $debug, "curlopts" => array(CURLOPT_USERAGENT => self::USER_AGENT)); + if (isset($ca_path)) { + $http_options["curlopts"][CURLOPT_CAPATH] = realpath($ca_path); + } + if (isset($ca_file)) { + $http_options["curlopts"][CURLOPT_CAINFO] = realpath($ca_file); + } + + $this->http = new Services_Zencoder_Http($api_host, $http_options); $this->accounts = new Services_Zencoder_Accounts($this); $this->inputs = new Services_Zencoder_Inputs($this); $this->jobs = new Services_Zencoder_Jobs($this); $this->notifications = new Services_Zencoder_Notifications($this); $this->outputs = new Services_Zencoder_Outputs($this); + $this->reports = new Services_Zencoder_Reports($this); } /** diff --git a/Services/Zencoder/Report.php b/Services/Zencoder/Report.php new file mode 100644 index 0000000..abc16df --- /dev/null +++ b/Services/Zencoder/Report.php @@ -0,0 +1,51 @@ + + * @version Release: 2.1.2 + * @license http://creativecommons.org/licenses/MIT/MIT + * @link http://github.com/zencoder/zencoder-php + */ +class Services_Zencoder_Report extends Services_Zencoder_Object { + + /** + * Statistics of a report + * + * @var object + */ + public $statistics; + + /** + * Totals of a of report + * + * @var object + */ + public $total; + + /** + * A copy of the raw API response for debug purposes + * + * @var mixed + */ + protected $raw_response; + + /** + * Create a new Services_Zencoder_Report object. + * For attributes of the various kinds of reports, see + * @link https://app.zencoder.com/docs/api/reports/vod + * @link https://app.zencoder.com/docs/api/reports/live + * @link https://app.zencoder.com/docs/api/reports/all + * + * @param mixed $params API response + * @param string $type The type of statistic we are fetching + */ + public function __construct($params) { + $this->raw_response = $params; + parent::__construct($params); + } + +} diff --git a/Services/Zencoder/Reports.php b/Services/Zencoder/Reports.php new file mode 100644 index 0000000..2d4d6fd --- /dev/null +++ b/Services/Zencoder/Reports.php @@ -0,0 +1,33 @@ + + * @version Release: 2.1.2 + * @license http://creativecommons.org/licenses/MIT/MIT + * @link http://github.com/zencoder/zencoder-php + */ +class Services_Zencoder_Reports extends Services_Zencoder_Base { + + /** + * Get details about different types of reports + * + * @link https://app.zencoder.com/docs/api/reports + * + * @param string $report_type The type of report to get. The following are currently supported over the api + * - 'all' : Returns all reports, both VOD and LIVE + * - 'vod' : Returns VOD reports + * - 'live': Return LIVE reports + * @param array $params An associated array of optional query parameters per requested report type + * @param array $opts Optional overrides + * + * @return Services_Zencoder_Report The object representation of the resource + */ + public function details($report_type = 'all', $params = array(), $opts = array()) { + return new Services_Zencoder_Report($this->proxy->retrieveData("reports/$report_type", $params, $opts)); + } + +}