22/*
33
44 Zencoder API PHP Library
5- Version: 1.0
5+ Version: 1.2
66 See the README file for info on how to use this library.
77
88*/
99define ('ZENCODER_LIBRARY_NAME ' ,"ZencoderPHP " );
10- define ('ZENCODER_LIBRARY_VERSION ' ,"1.0 " );
10+ define ('ZENCODER_LIBRARY_VERSION ' ,"1.2 " );
1111
1212// Add JSON functions for PHP < 5.2.0
1313if (!function_exists ('json_encode ' )) {
14- require_once (' lib/JSON.php ' );
14+ require_once (dirname ( __FILE__ ) . ' / lib/JSON.php ' );
1515$ GLOBALS ['JSON_OBJECT ' ] =new Services_JSON (SERVICES_JSON_LOOSE_TYPE );
1616function json_encode ($ value ) {return $ GLOBALS ['JSON_OBJECT ' ]->encode ($ value ); }
1717function json_decode ($ value ) {return $ GLOBALS ['JSON_OBJECT ' ]->decode ($ value ); }
1818}
1919
20+ // Check that cURL extension is enabled
21+ if (!function_exists ('curl_init ' )) {
22+ throw new Exception ('You must have the cURL extension enabled to use this library. ' );
23+ }
24+
2025class ZencoderJob {
2126
22- var$ new_job_url ="https://app.zencoder.com/api/jobs " ;
27+ var$ new_job_url ="https://app.zencoder.com/api/v1/ jobs " ;
2328 var$ new_job_params =array ();
2429 var$ created =false ;
2530 var$ errors =array ();
@@ -35,12 +40,12 @@ class ZencoderJob {
3540function ZencoderJob ($ params ,$ options =array ()) {
3641
3742// Build using params if not sending request
38- if ($ options ["build " ]) {
43+ if (! empty ( $ options ["build " ]) ) {
3944$ this ->update_attributes ($ params );
4045return true ;
4146 }
4247
43- if ($ options ["url " ])$ this ->new_job_url =$ options ["url " ];
48+ if (! empty ( $ options ["url " ]) )$ this ->new_job_url =$ options ["url " ];
4449$ this ->new_job_params =$ params ;
4550$ this ->created =$ this ->create ();
4651 }
@@ -75,7 +80,7 @@ function update_attributes($attributes = array()) {
7580// Use the Label for the key if avaiable.
7681function create_outputs ($ outputs =array ()) {
7782foreach ($ outputsas $ output_attrs ) {
78- if ($ output_attrs ["label " ]) {
83+ if (! empty ( $ output_attrs ["label " ]) ) {
7984$ this ->outputs [$ output_attrs ["label " ]] =new ZencoderOutputFile ($ output_attrs );
8085 }else {
8186$ this ->outputs [] =new ZencoderOutputFile ($ output_attrs );
@@ -172,10 +177,9 @@ class ZencoderCURL {
172177CURLOPT_HEADER =>0 ,// Don't return the header in result
173178CURLOPT_HTTPHEADER =>array ("Content-Type: application/json " ,"Accept: application/json " ),
174179CURLOPT_CONNECTTIMEOUT =>0 ,// Time in seconds to timeout send request. 0 is no timeout.
175- // CURLOPT_FOLLOWLOCATION => 0, // Follow redirects. (stopped because it was causing an error in safe mode)
176- // Dealing with the certificate. Still a sketchy area.
177- CURLOPT_SSL_VERIFYPEER =>0 ,// Turn off verification, curl -k or --insecure
178- CURLOPT_SSL_VERIFYHOST =>0
180+ CURLOPT_FOLLOWLOCATION =>1 ,// Follow redirects.
181+ CURLOPT_SSL_VERIFYPEER =>1 ,
182+ CURLOPT_SSL_VERIFYHOST =>1
179183 );
180184
181185 var$ connected ;
@@ -186,6 +190,11 @@ class ZencoderCURL {
186190// Initialize
187191function ZencoderCURL ($ url ,$ json ,$ options =array ()) {
188192
193+ // If PHP in safe mode, disable following location
194+ if (ini_get ('safe_mode ' ) ) {
195+ $ this ->options [CURLOPT_FOLLOWLOCATION ] =0 ;
196+ }
197+
189198// Add library details to request
190199$ this ->options [CURLOPT_HTTPHEADER ][] ="Zencoder-Library-Name: " .ZENCODER_LIBRARY_NAME ;
191200$ this ->options [CURLOPT_HTTPHEADER ][] ="Zencoder-Library-Version: " .ZENCODER_LIBRARY_VERSION ;
@@ -209,6 +218,13 @@ function ZencoderCURL($url, $json, $options = array()) {
209218
210219// Execute session and store returned results
211220$ this ->results =curl_exec ($ ch );
221+
222+ // Code based on Facebook PHP SDK
223+ // Retries request if unable to validate cert chain
224+ if (curl_errno ($ ch ) ==60 ) {// CURLE_SSL_CACERT
225+ curl_setopt ($ ch ,CURLOPT_CAINFO ,dirname (__FILE__ ) .'/lib/zen_ca_chain.crt ' );
226+ $ this ->results =curl_exec ($ ch );
227+ }
212228
213229// Store the HTTP status code given (201, 404, etc.)
214230$ this ->status_code =curl_getinfo ($ ch ,CURLINFO_HTTP_CODE );
@@ -233,8 +249,8 @@ class ZencoderOutputNotification {
233249 var$ job ;
234250
235251function ZencoderOutputNotification ($ params ) {
236- if ($ params ["output " ])$ this ->output =new ZencoderOutputFile ($ params ["output " ]);
237- if ($ params ["job " ])$ this ->job =new ZencoderJob ($ params ["job " ],array ("build " =>true ));
252+ if (! empty ( $ params ["output " ]) )$ this ->output =new ZencoderOutputFile ($ params ["output " ]);
253+ if (! empty ( $ params ["job " ]) )$ this ->job =new ZencoderJob ($ params ["job " ],array ("build " =>true ));
238254 }
239255
240256function catch_and_parse () {