@@ -34,171 +34,173 @@ class Services_Zencoder_Exception extends ErrorException {}
3434
3535class Services_Zencoderextends Services_Zencoder_Base
3636{
37- const USER_AGENT ='ZencoderPHP v2.0 ' ;
37+ const USER_AGENT ='ZencoderPHP v2.0 ' ;
3838
39- /**
40- * Contains the HTTP communication class
41- */
42- protected $ http ;
43- /**
44- * Contains the default API version
45- */
46- protected $ version ;
39+ /**
40+ * Contains the HTTP communication class
41+ */
42+ protected $ http ;
43+ /**
44+ * Contains the default API version
45+ */
46+ protected $ version ;
4747
48- /**
49- * Initialize the Services_Zencoder class and sub-classes.
50- *
51- * @param string $api_key API Key
52- * @param string $api_version API version
53- * @param string $api_host API host
54- */
55- public function __construct (
56- $ api_key =NULL ,
57- $ api_version ='v2 ' ,
58- $ api_host ='https://app.zencoder.com '
59- ) {
60- // Check that library dependencies are met
61- if (strnatcmp (phpversion (),'5.2.0 ' ) <0 ) {
62- throw new Services_Zencoder_Exception ('PHP version 5.2 or higher is required. ' );
48+ /**
49+ * Initialize the Services_Zencoder class and sub-classes.
50+ *
51+ * @param string $api_key API Key
52+ * @param string $api_version API version
53+ * @param string $api_host API host
54+ */
55+ public function __construct (
56+ $ api_key =NULL ,
57+ $ api_version ='v2 ' ,
58+ $ api_host ='https://app.zencoder.com '
59+ )
60+ {
61+ // Check that library dependencies are met
62+ if (strnatcmp (phpversion (),'5.2.0 ' ) <0 ) {
63+ throw new Services_Zencoder_Exception ('PHP version 5.2 or higher is required. ' );
64+ }
65+ if (!function_exists ('json_encode ' )) {
66+ throw new Services_Zencoder_Exception ('JSON support must be enabled. ' );
67+ }
68+ if (!function_exists ('curl_init ' )) {
69+ throw new Services_Zencoder_Exception ('cURL extension must be enabled. ' );
70+ }
71+ $ this ->version =$ api_version ;
72+ $ this ->http =new Services_Zencoder_Http (
73+ $ api_host ,
74+ array ("curlopts " =>array (
75+ CURLOPT_USERAGENT =>self ::USER_AGENT ,
76+ CURLOPT_CAINFO =>dirname (__FILE__ ) ."/zencoder_ca_chain.crt " ,
77+ ),"api_key " =>$ api_key )
78+ );
79+ $ this ->accounts =new Services_Zencoder_Accounts ($ this );
80+ $ this ->inputs =new Services_Zencoder_Inputs ($ this );
81+ $ this ->jobs =new Services_Zencoder_Jobs ($ this );
82+ $ this ->notifications =new Services_Zencoder_Notifications ($ this );
83+ $ this ->outputs =new Services_Zencoder_Outputs ($ this );
6384 }
64- if (!function_exists ('json_encode ' )) {
65- throw new Services_Zencoder_Exception ('JSON support must be enabled. ' );
66- }
67- if (!function_exists ('curl_init ' )) {
68- throw new Services_Zencoder_Exception ('cURL extension must be enabled. ' );
85+
86+ /**
87+ * GET the resource at the specified path.
88+ *
89+ * @param string $path Path to the resource
90+ * @param array $params Query string parameters
91+ * @param array $opts Optional overrides
92+ *
93+ * @return object The object representation of the resource
94+ */
95+ public function retrieveData ($ path ,array $ params =array (),array $ opts =array ())
96+ {
97+ return empty ($ params )
98+ ?$ this ->_processResponse ($ this ->http ->get ($ this ->_getApiPath ($ opts ) .$ path ))
99+ :$ this ->_processResponse (
100+ $ this ->http ->get ($ this ->_getApiPath ($ opts ) .$ path ."? " .http_build_query ($ params ,'' ,'& ' ))
101+ );
69102 }
70- $ this ->version =$ api_version ;
71- $ this ->http =new Services_Zencoder_Http (
72- $ api_host ,
73- array ("curlopts " =>array (
74- CURLOPT_USERAGENT =>self ::USER_AGENT ,
75- CURLOPT_CAINFO =>dirname (__FILE__ ) ."/zencoder_ca_chain.crt " ,
76- ),"api_key " =>$ api_key )
77- );
78- $ this ->accounts =new Services_Zencoder_Accounts ($ this );
79- $ this ->inputs =new Services_Zencoder_Inputs ($ this );
80- $ this ->jobs =new Services_Zencoder_Jobs ($ this );
81- $ this ->notifications =new Services_Zencoder_Notifications ($ this );
82- $ this ->outputs =new Services_Zencoder_Outputs ($ this );
83- }
84103
85- /**
86- * GET the resource at the specified path.
87- *
88- * @param string $path Path to the resource
89- * @param array $params Query string parameters
90- * @param array $opts Optional overrides
91- *
92- * @return object The object representation of the resource
93- */
94- public function retrieveData ($ path ,array $ params =array (),array $ opts =array ())
95- {
96- return empty ($ params )
97- ?$ this ->_processResponse ($ this ->http ->get ($ this ->_getApiPath ($ opts ) .$ path ))
98- :$ this ->_processResponse (
99- $ this ->http ->get ($ this ->_getApiPath ($ opts ) .$ path ."? " .http_build_query ($ params ,'' ,'& ' ))
100- );
101- }
104+ /**
105+ * DELETE the resource at the specified path.
106+ *
107+ * @param string $path Path to the resource
108+ * @param array $params Query string parameters
109+ * @param array $opts Optional overrides
110+ *
111+ * @return object The object representation of the resource
112+ */
113+ public function deleteData ($ path ,array $ opts =array ())
114+ {
115+ return $ this ->_processResponse ($ this ->http ->delete ($ this ->_getApiPath ($ opts ) .$ path ));
116+ }
102117
103- /**
104- * DELETE the resource at the specified path.
105- *
106- * @param string $path Path to the resource
107- * @param array $params Query string parameters
108- * @param array $opts Optional overrides
109- *
110- * @return object The object representation of the resource
111- */
112- public function deleteData ($ path ,array $ opts =array ())
113- {
114- return $ this ->_processResponse ($ this ->http ->delete ($ this ->_getApiPath ($ opts ) .$ path ));
115- }
118+ /**
119+ * POST to the resource at the specified path.
120+ *
121+ * @param string $path Path to the resource
122+ * @param array $params Query string parameters
123+ * @param array $opts Optional overrides
124+ *
125+ * @return object The object representation of the resource
126+ */
127+ public function createData ($ path ,$ body ="" ,array $ opts =array ())
128+ {
129+ $ headers =array ('Content-Type ' =>'application/json ' );
130+ return empty ($ body )
131+ ?$ this ->_processResponse ($ this ->http ->post ($ this ->_getApiPath ($ opts ) .$ path ,$ headers ))
132+ :$ this ->_processResponse (
133+ $ this ->http ->post (
134+ $ this ->_getApiPath ($ opts ) .$ path ,
135+ $ headers ,
136+ $ body
137+ )
138+ );
139+ }
116140
117- /**
118- * POST to the resource at the specified path.
119- *
120- * @param string $path Path to the resource
121- * @param array $params Query string parameters
122- * @param array $opts Optional overrides
123- *
124- * @return object The object representation of the resource
125- */
126- public function createData ($ path ,$ body ="" ,array $ opts =array ())
127- {
128- $ headers =array ('Content-Type ' =>'application/json ' );
129- return empty ($ body )
130- ?$ this ->_processResponse ($ this ->http ->post ($ this ->_getApiPath ($ opts ) .$ path ,$ headers ))
131- :$ this ->_processResponse (
132- $ this ->http ->post (
133- $ this ->_getApiPath ($ opts ) .$ path ,
134- $ headers ,
135- $ body
136- )
137- );
138- }
141+ /**
142+ * PUT to the resource at the specified path.
143+ *
144+ * @param string $path Path to the resource
145+ * @param array $params Query string parameters
146+ * @param array $opts Optional overrides
147+ *
148+ * @return object The object representation of the resource
149+ */
150+ public function updateData ($ path ,$ body ="" ,array $ opts =array ())
151+ {
152+ $ headers =array ('Content-Type ' =>'application/json ' );
153+ return empty ($ params )
154+ ?$ this ->_processResponse ($ this ->http ->put ($ this ->_getApiPath ($ opts ) .$ path ,$ headers ))
155+ :$ this ->_processResponse (
156+ $ this ->http ->put (
157+ $ this ->_getApiPath ($ opts ) .$ path ,
158+ $ headers ,
159+ $ body
160+ )
161+ );
162+ }
139163
140- /**
141- * PUT to the resource at the specified path.
142- *
143- * @param string $path Path to the resource
144- * @param array $params Query string parameters
145- * @param array $opts Optional overrides
146- *
147- * @return object The object representation of the resource
148- */
149- public function updateData ($ path ,$ body ="" ,array $ opts =array ())
150- {
151- $ headers =array ('Content-Type ' =>'application/json ' );
152- return empty ($ params )
153- ?$ this ->_processResponse ($ this ->http ->put ($ this ->_getApiPath ($ opts ) .$ path ,$ headers ))
154- :$ this ->_processResponse (
155- $ this ->http ->put (
156- $ this ->_getApiPath ($ opts ) .$ path ,
157- $ headers ,
158- $ body
164+ private function _getApiPath ($ opts =array ())
165+ {
166+ return isset ($ opts ['no_transform ' ])
167+ ?""
168+ :"/api/ " . (
169+ isset ($ opts ['api_version ' ])
170+ ?$ opts ['api_version ' ]
171+ :$ this ->version
159172 )
160- );
161- }
162-
163- private function _getApiPath ($ opts =array ())
164- {
165- return isset ($ opts ['no_transform ' ])
166- ?""
167- :"/api/ " . (
168- isset ($ opts ['api_version ' ])
169- ?$ opts ['api_version ' ]
170- :$ this ->version
171- )
172- ."/ " ;
173- }
174-
175- private function _processResponse ($ response )
176- {
177- list ($ status ,$ headers ,$ body ) =$ response ;
178- if ($ status ==204 || (($ status ==200 ||$ status ==201 ) &&trim ($ body ) =="" )) {
179- return TRUE ;
173+ ."/ " ;
180174 }
181- if (empty ($ headers ['Content-Type ' ])) {
182- throw new Services_Zencoder_Exception ('Response header is missing Content-Type ' );
183- }
184- switch ($ headers ['Content-Type ' ]) {
185- case 'application/json ' :
186- case 'application/json; charset=utf-8 ' :
187- return $ this ->_processJsonResponse ($ status ,$ headers ,$ body );
188- break ;
175+
176+ private function _processResponse ($ response )
177+ {
178+ list ($ status ,$ headers ,$ body ) =$ response ;
179+ if ($ status ==204 || (($ status ==200 ||$ status ==201 ) &&trim ($ body ) =="" )) {
180+ return TRUE ;
181+ }
182+ if (empty ($ headers ['Content-Type ' ])) {
183+ throw new Services_Zencoder_Exception ('Response header is missing Content-Type ' );
184+ }
185+ switch ($ headers ['Content-Type ' ]) {
186+ case 'application/json ' :
187+ case 'application/json; charset=utf-8 ' :
188+ return $ this ->_processJsonResponse ($ status ,$ headers ,$ body );
189+ break ;
190+ }
191+ throw new Services_Zencoder_Exception (
192+ 'Unexpected content type: ' .$ headers ['Content-Type ' ]);
189193 }
190- throw new Services_Zencoder_Exception (
191- 'Unexpected content type: ' .$ headers ['Content-Type ' ]);
192- }
193194
194- private function _processJsonResponse ($ status ,$ headers ,$ body ) {
195- $ decoded =json_decode ($ body );
196- if ($ status >=200 &&$ status <300 ) {
197- return $ decoded ;
195+ private function _processJsonResponse ($ status ,$ headers ,$ body )
196+ {
197+ $ decoded =json_decode ($ body );
198+ if ($ status >=200 &&$ status <300 ) {
199+ return $ decoded ;
200+ }
201+ throw new Services_Zencoder_Exception (
202+ "Invalid HTTP status code: " .$ status
203+ .", body: " .$ body
204+ );
198205 }
199- throw new Services_Zencoder_Exception (
200- "Invalid HTTP status code: " .$ status
201- .", body: " .$ body
202- );
203- }
204206}