2
2
/*
3
3
4
4
Zencoder API PHP Library
5
- Version: 1.0
5
+ Version: 1.2
6
6
See the README file for info on how to use this library.
7
7
8
8
*/
9
9
define ('ZENCODER_LIBRARY_NAME ' , "ZencoderPHP " );
10
- define ('ZENCODER_LIBRARY_VERSION ' , "1.0 " );
10
+ define ('ZENCODER_LIBRARY_VERSION ' , "1.2 " );
11
11
12
12
// Add JSON functions for PHP < 5.2.0
13
13
if (!function_exists ('json_encode ' )) {
14
- require_once (' lib/JSON.php ' );
14
+ require_once (dirname ( __FILE__ ) . ' / lib/JSON.php ' );
15
15
$ GLOBALS ['JSON_OBJECT ' ] = new Services_JSON (SERVICES_JSON_LOOSE_TYPE );
16
16
function json_encode ($ value ) { return $ GLOBALS ['JSON_OBJECT ' ]->encode ($ value ); }
17
17
function json_decode ($ value ) { return $ GLOBALS ['JSON_OBJECT ' ]->decode ($ value ); }
18
18
}
19
19
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
+
20
25
class ZencoderJob {
21
26
22
- var $ new_job_url = "https://app.zencoder.com/api/jobs " ;
27
+ var $ new_job_url = "https://app.zencoder.com/api/v1/ jobs " ;
23
28
var $ new_job_params = array ();
24
29
var $ created = false ;
25
30
var $ errors = array ();
@@ -35,12 +40,12 @@ class ZencoderJob {
35
40
function ZencoderJob ($ params , $ options = array ()) {
36
41
37
42
// Build using params if not sending request
38
- if ($ options ["build " ]) {
43
+ if (! empty ( $ options ["build " ]) ) {
39
44
$ this ->update_attributes ($ params );
40
45
return true ;
41
46
}
42
47
43
- if ($ options ["url " ]) $ this ->new_job_url = $ options ["url " ];
48
+ if (! empty ( $ options ["url " ]) ) $ this ->new_job_url = $ options ["url " ];
44
49
$ this ->new_job_params = $ params ;
45
50
$ this ->created = $ this ->create ();
46
51
}
@@ -75,7 +80,7 @@ function update_attributes($attributes = array()) {
75
80
// Use the Label for the key if avaiable.
76
81
function create_outputs ($ outputs = array ()) {
77
82
foreach ($ outputs as $ output_attrs ) {
78
- if ($ output_attrs ["label " ]) {
83
+ if (! empty ( $ output_attrs ["label " ]) ) {
79
84
$ this ->outputs [$ output_attrs ["label " ]] = new ZencoderOutputFile ($ output_attrs );
80
85
} else {
81
86
$ this ->outputs [] = new ZencoderOutputFile ($ output_attrs );
@@ -172,10 +177,9 @@ class ZencoderCURL {
172
177
CURLOPT_HEADER => 0 , // Don't return the header in result
173
178
CURLOPT_HTTPHEADER => array ("Content-Type: application/json " , "Accept: application/json " ),
174
179
CURLOPT_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
179
183
);
180
184
181
185
var $ connected ;
@@ -186,6 +190,11 @@ class ZencoderCURL {
186
190
// Initialize
187
191
function ZencoderCURL ($ url , $ json , $ options = array ()) {
188
192
193
+ // If PHP in safe mode, disable following location
194
+ if ( ini_get ('safe_mode ' ) ) {
195
+ $ this ->options [CURLOPT_FOLLOWLOCATION ] = 0 ;
196
+ }
197
+
189
198
// Add library details to request
190
199
$ this ->options [CURLOPT_HTTPHEADER ][] = "Zencoder-Library-Name: " .ZENCODER_LIBRARY_NAME ;
191
200
$ this ->options [CURLOPT_HTTPHEADER ][] = "Zencoder-Library-Version: " .ZENCODER_LIBRARY_VERSION ;
@@ -209,6 +218,13 @@ function ZencoderCURL($url, $json, $options = array()) {
209
218
210
219
// Execute session and store returned results
211
220
$ 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
+ }
212
228
213
229
// Store the HTTP status code given (201, 404, etc.)
214
230
$ this ->status_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
@@ -233,8 +249,8 @@ class ZencoderOutputNotification {
233
249
var $ job ;
234
250
235
251
function 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 ));
238
254
}
239
255
240
256
function catch_and_parse () {
0 commit comments