Skip to content

Commit 6935012

Browse files
committed
Added General API Requests
Switched Jobs to use general API requests.
1 parent 4e2c12e commit 6935012

File tree

2 files changed

+126
-56
lines changed

2 files changed

+126
-56
lines changed

README.markdown

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Zencoder API PHP Library
33

44
Author: [Steve Heffernan](http://www.steveheffernan.com) (steve (a) zencoder (.) com)
55
Company: [Zencoder - Online Video Encoder](http://zencoder.com)
6-
Version: 1.0
7-
Date: 2010-04-02
6+
Version: 1.1
7+
Date: 2010-06-04
88
Repository: <http://github.com/zencoder/zencoder-php/>
99

1010
For more details on the Zencoder API requirements visit
@@ -85,6 +85,49 @@ The previous JSON example would become:
8585
));
8686

8787

88+
GENERAL API REQUESTS
89+
--------------------
90+
A general API request can be used for all API functionality including **Job Listing**, **Job Details**, **Account Creation**, **Account Details** (even Job Creation if desired). See the [API docs](http://zencoder.com/docs/api/) for all possible API requests.
91+
The first argument is the **API URL**.
92+
The second argument is your **API Key**.
93+
The third argument is the **request parameters** if needed. It can either be a JSON string or an array of parameters.
94+
95+
96+
#### Example Job List Request
97+
98+
$request = new ZencoderRequest(
99+
'https://app.zencoder/api/jobs',
100+
'93h630j1dsyshjef620qlkavnmzui3'
101+
);
102+
103+
if ($request->successful) {
104+
print_r($request->results);
105+
} else {
106+
foreach($request->errors as $error) {
107+
echo $error."\n";
108+
}
109+
}
110+
111+
#### Example Account Creation Request
112+
113+
$request = new ZencoderRequest(
114+
'https://app.zencoder/api/account',
115+
false, // API key isn't needed for new account creation
116+
array(
117+
"terms_of_service" => "1",
118+
"email" => "test@example.com",
119+
"password" => "1234"
120+
)
121+
);
122+
123+
if ($request->successful) {
124+
print_r($request->results);
125+
} else {
126+
foreach($request->errors as $error) {
127+
echo $error."\n";
128+
}
129+
}
130+
88131

89132
NOTIFICATION HANDLING
90133
----------------------
@@ -152,5 +195,5 @@ Your [notifications page](https://app.zencoder.com/notifications) will come in h
152195

153196
VERSIONS
154197
---------
155-
198+
Version 1.1 - 2010-06-04 Added General API Requests
156199
Version 1.0 - 2010-04-02 Jobs and Notifications.

Zencoder.php

Lines changed: 80 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function json_decode($value) { return $GLOBALS['JSON_OBJECT']->decode($value); }
1919

2020
class ZencoderJob {
2121

22-
var $create_url = "https://app.zencoder.com/api/jobs";
23-
var $create_params = array();
24-
var $create_json;
22+
var $new_job_url = "https://zencoder-staging.heroku.com/api/jobs";
23+
var $new_job_params = array();
24+
var $new_job_json;
2525
var $created = false;
2626
var $errors = array();
2727

@@ -38,46 +38,20 @@ function ZencoderJob($params, $options = array()) {
3838
return true;
3939
}
4040

41-
// Get JSON
42-
if(is_string($params)) {
43-
$this->create_json = trim($params);
44-
$this->create_params = json_decode($params, true);
45-
} else if(is_array($params)) {
46-
$this->create_json = json_encode($params);
47-
$this->create_params = $params;
48-
}
49-
41+
$this->new_job_params = $params;
5042
$this->created = $this->create();
5143
}
5244

5345
// Send Job Request to API
5446
function create() {
55-
5647
// Send request
57-
$connection = new ZencoderCURL($this->create_url, $this->create_json);
58-
59-
// Check for connection errors
60-
if ($connection->connected == false) {
61-
$this->errors[] = $connection->error;
62-
return false;
63-
}
64-
65-
// Parse returned JSON
66-
$parsed_results = json_decode($connection->results, true);
48+
$request = new ZencoderRequest($this->new_job_url, false, $this->new_job_params);
6749

68-
// Return based on HTTP status code
69-
if($connection->status_code == "201") {
70-
$this->update_attributes($parsed_results);
50+
if($request->successful) {
51+
$this->update_attributes($request->results);
7152
return true;
7253
} else {
73-
// Get job request errors if any
74-
if(is_array($parsed_results["errors"])) {
75-
foreach($parsed_results["errors"] as $error) {
76-
$this->errors[] = $error;
77-
}
78-
} else {
79-
$this->errors[] = "Unknown Error\n\nHTTP Status Code: ".$connection->status_code."\n";"Raw Results: \n".$connection->results;
80-
}
54+
$this->errors = array_merge($this->errors, $request->errors);
8155
return false;
8256
}
8357
}
@@ -107,7 +81,6 @@ function create_outputs($outputs = array()) {
10781
}
10882
}
10983

110-
11184
class ZencoderOutputFile {
11285

11386
var $id;
@@ -131,28 +104,66 @@ function update_attributes($attributes = array()) {
131104
}
132105
}
133106

107+
// General API request class
108+
class ZencoderRequest {
134109

135-
class ZencoderOutputNotification {
110+
var $successful = false;
111+
var $errors = array();
112+
var $raw_results;
113+
var $results;
136114

137-
var $output;
138-
var $job;
115+
function ZencoderRequest($url, $api_key = "", $params = "") {
139116

140-
function ZencoderOutputNotification($params) {
141-
if($params["output"]) $this->output = new ZencoderOutputFile($params["output"]);
142-
if($params["job"]) $this->job = new ZencoderJob($params["job"], array("build" => true));
143-
}
117+
// Add api_key to url if supplied
118+
if($api_key) {
119+
$url .= "?api_key=".$api_key;
120+
}
144121

145-
function catch_and_parse() {
146-
$notificiation_data = json_decode(trim(file_get_contents('php://input')), true);
147-
return new ZencoderOutputNotification($notificiation_data);
122+
// Get JSON
123+
if(is_string($params)) {
124+
$json = trim($params);
125+
} else if(is_array($params)) {
126+
$json = json_encode($params);
127+
} else {
128+
$json = false;
129+
}
130+
131+
// Create request
132+
$request = new ZencoderCURL($url, $json);
133+
134+
// Check for connection errors
135+
if ($request->connected == false) {
136+
$this->errors[] = $request->error;
137+
return;
138+
}
139+
140+
$status_code = intval($request->status_code);
141+
$this->raw_results = $request->results;
142+
143+
// Parse returned JSON
144+
$this->results = json_decode($this->raw_results, true);
145+
146+
// Return based on HTTP status code
147+
if($status_code >= 200 && $status_code <= 206) {
148+
$this->successful = true;
149+
} else {
150+
// Get job request errors if any
151+
if(is_array($this->results["errors"])) {
152+
foreach($this->results["errors"] as $error) {
153+
$this->errors[] = $error;
154+
}
155+
} else {
156+
$this->errors[] = "Unknown Error\n\nHTTP Status Code: ".$request->status_code."\n"."Raw Results: \n".$request->raw_results;
157+
}
158+
}
148159
}
149160
}
150161

151-
152-
// Connection class
162+
// ZencoderCURL
163+
// The connection class to perform the actual request to the surver
164+
// using cURL http://php.net/manual/en/book.curl.php
153165
class ZencoderCURL {
154166

155-
var $url;
156167
var $options = array(
157168
CURLOPT_RETURNTRANSFER => 1, // Return content of the url
158169
CURLOPT_HEADER => 0, // Don't return the header in result
@@ -167,17 +178,16 @@ class ZencoderCURL {
167178
var $error;
168179

169180
// Initialize
170-
function ZencoderCURL($url, $data, $options = array()) {
171-
$this->url = $url;
181+
function ZencoderCURL($url, $json, $options = array()) {
172182

173183
// Add library details to request
174184
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Name: ".ZENCODER_LIBRARY_NAME;
175185
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Version: ".ZENCODER_LIBRARY_VERSION;
176186

177187
// If posting data
178-
if($data) {
188+
if($json) {
179189
$this->options[CURLOPT_POST] = 1;
180-
$this->options[CURLOPT_POSTFIELDS] = $data;
190+
$this->options[CURLOPT_POSTFIELDS] = $json;
181191
}
182192

183193
// Add cURL options to defaults (can't use array_merge)
@@ -186,7 +196,7 @@ function ZencoderCURL($url, $data, $options = array()) {
186196
}
187197

188198
// Initialize session
189-
$ch = curl_init($this->url);
199+
$ch = curl_init($url);
190200

191201
// Set transfer options
192202
curl_setopt_array($ch, $this->options);
@@ -209,3 +219,20 @@ function ZencoderCURL($url, $data, $options = array()) {
209219
curl_close($ch);
210220
}
211221
}
222+
223+
// Capture incoming notifications from Zencoder to your app
224+
class ZencoderOutputNotification {
225+
226+
var $output;
227+
var $job;
228+
229+
function ZencoderOutputNotification($params) {
230+
if($params["output"]) $this->output = new ZencoderOutputFile($params["output"]);
231+
if($params["job"]) $this->job = new ZencoderJob($params["job"], array("build" => true));
232+
}
233+
234+
function catch_and_parse() {
235+
$notificiation_data = json_decode(trim(file_get_contents('php://input')), true);
236+
return new ZencoderOutputNotification($notificiation_data);
237+
}
238+
}

0 commit comments

Comments
 (0)