Skip to content

Commit ee22220

Browse files
author
Michael Christopher
committed
Initial commit for v2.0 of the PHP API. Many bugs to work out before launch.
1 parent 24429f7 commit ee22220

18 files changed

+548
-1194
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.php

README.markdown renamed to README

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Zencoder API PHP Library
22
==========================
33

4-
Author: [Steve Heffernan](http://www.steveheffernan.com) (steve (a) zencoder (.) com)
5-
Company: [Zencoder - Online Video Encoder](http://zencoder.com)
6-
Version: 1.3
7-
Date: 2011-09-21
4+
Author: [Michael Christopher] (m (a) zencoder (.) com)
5+
Company: [Zencoder - Online Video Encoder](http://www.zencoder.com)
6+
Version: 2.0
7+
Date: 2011-12-05
88
Repository: <http://github.com/zencoder/zencoder-php/>
99

1010
For more details on the Zencoder API requirements visit
11-
<http://zencoder.com/docs/api>
11+
<http://app.zencoder.com/docs/api>
1212

1313

1414
ENCODING JOB
@@ -29,12 +29,14 @@ and pass it as the parameters for a new ZencoderJob object. Execute the script o
2929
<?php
3030

3131
// Make sure this points to a copy of Zencoder.php on the same server as this script.
32-
require_once("zencoder-php/Zencoder.php");
32+
require_once('Services/Zencoder.php');
33+
34+
// Initialize the Services_Zencoder class
35+
$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');
3336

3437
// New Encoding Job
35-
$encoding_job = new ZencoderJob('
38+
$encoding_job = $zencoder->jobs->create('
3639
{
37-
"api_key": "93h630j1dsyshjef620qlkavnmzui3",
3840
"input": "s3://bucket-name/file-name.avi",
3941
"outputs": [
4042
{
@@ -45,11 +47,11 @@ and pass it as the parameters for a new ZencoderJob object. Execute the script o
4547
');
4648

4749
// Check if it worked
48-
if ($encoding_job->created) {
50+
if ($encoding_job) {
4951
// Success
5052
echo "w00t! \n\n";
5153
echo "Job ID: ".$encoding_job->id."\n";
52-
echo "Output '".$encoding_job->outputs["web"]->label."' ID: ".$encoding_job->outputs["web"]->id."\n";
54+
echo "Output '".$encoding_job->outputs[0]->label."' ID: ".$encoding_job->outputs[0]->id."\n";
5355
// Store Job/Output IDs to update their status when notified or to check their progress.
5456
} else {
5557
// Failed
@@ -74,8 +76,7 @@ You can revisit your [API builder](https://app.zencoder.com/api_builder) to add/
7476
You can translate the JSON string into nested associative arrays so that you can dynamically change attributes like "input".
7577
The previous JSON example would become:
7678

77-
$encoding_job = new ZencoderJob(array(
78-
"api_key" => "93h630j1dsyshjef620qlkavnmzui3",
79+
$encoding_job = $zencoder->jobs->create(array(
7980
"input" => "s3://bucket-name/file-name.avi",
8081
"outputs" => array(
8182
array(
@@ -85,50 +86,6 @@ The previous JSON example would become:
8586
));
8687

8788

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.com/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.com/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-
131-
13289
NOTIFICATION HANDLING
13390
----------------------
13491
The ZencoderOutputNotification class is used to capture and parse JSON data sent from
@@ -195,6 +152,7 @@ Your [notifications page](https://app.zencoder.com/notifications) will come in h
195152

196153
VERSIONS
197154
---------
155+
Version 2.0 - 2011-12-02 Complete refactoring of library
198156
Version 1.6 - 2011-10-24 Fixed issue with user agents in cURL
199157
Version 1.4 - 2011-10-06 Fixed error with adding api_key to URL
200158
Version 1.3 - 2011-09-21 Fixed bundled SSL certification chain and made catch_and_parse() static

Services/Zencoder.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/*
3+
4+
Zencoder API PHP Library
5+
Version: 2.0
6+
See the README file for info on how to use this library.
7+
8+
*/
9+
10+
class Services_Zencoder_Exception extends ErrorException {}
11+
12+
function Services_Zencoder_autoload($className) {
13+
if (substr($className, 0, 17) != 'Services_Zencoder') {
14+
return false;
15+
}
16+
$file = str_replace('_', '/', $className);
17+
$file = str_replace('Services/', '', $file);
18+
return include dirname(__FILE__) . "/$file.php";
19+
}
20+
21+
spl_autoload_register('Services_Zencoder_autoload');
22+
23+
/**
24+
* Zencoder API client interface.
25+
*
26+
* @category Services
27+
* @package Services_Zencoder
28+
* @author Michael Christopher <m@zencoder.com>
29+
* @license http://creativecommons.org/licenses/MIT/MIT
30+
* @link http://github.com/zencoder/zencoder-php
31+
*/
32+
class Services_Zencoder extends Services_Zencoder_Base
33+
{
34+
const USER_AGENT = 'ZencoderPHP v2.0';
35+
36+
protected $http;
37+
protected $version;
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param string $api_key API Key
43+
* @param string $api_version API version
44+
* @param string $api_host API host
45+
*/
46+
public function __construct(
47+
$api_key = NULL,
48+
$api_version = 'v2',
49+
$api_host = 'https://app.zencoder.com'
50+
) {
51+
$this->version = $api_version;
52+
$this->http = new Services_Zencoder_Http(
53+
$api_host,
54+
array("curlopts" => array(
55+
CURLOPT_USERAGENT => self::USER_AGENT,
56+
CURLOPT_CAINFO => dirname(__FILE__) . "/zencoder_ca_chain.crt",
57+
), "api_key" => $api_key)
58+
);
59+
$this->accounts = new Services_Zencoder_Accounts($this);
60+
$this->inputs = new Services_Zencoder_Inputs($this);
61+
$this->jobs = new Services_Zencoder_Jobs($this);
62+
$this->notifications = new Services_Zencoder_Notifications($this);
63+
$this->outputs = new Services_Zencoder_Outputs($this);
64+
}
65+
66+
/**
67+
* GET the resource at the specified path.
68+
*
69+
* @param string $path Path to the resource
70+
* @param array $params Query string parameters
71+
*
72+
* @return object The object representation of the resource
73+
*/
74+
public function retrieveData($path, array $params = array())
75+
{
76+
$path = "/api/$this->version/$path.json";
77+
return empty($params)
78+
? $this->_processResponse($this->http->get($path))
79+
: $this->_processResponse(
80+
$this->http->get("$path?" . http_build_query($params, '', '&'))
81+
);
82+
}
83+
84+
/**
85+
* DELETE the resource at the specified path.
86+
*
87+
* @param string $path Path to the resource
88+
* @param array $params Query string parameters
89+
*
90+
* @return object The object representation of the resource
91+
*/
92+
public function deleteData($path)
93+
{
94+
$path = "/api/$this->version/$path.json";
95+
return $this->_processResponse($this->http->delete($path));
96+
}
97+
98+
/**
99+
* POST to the resource at the specified path.
100+
*
101+
* @param string $path Path to the resource
102+
* @param array $params Query string parameters
103+
*
104+
* @return object The object representation of the resource
105+
*/
106+
public function createData($path, $body = "")
107+
{
108+
$path = "/api/$this->version/$path";
109+
$headers = array('Content-Type' => 'application/json');
110+
return empty($body)
111+
? $this->_processResponse($this->http->post($path, $headers))
112+
: $this->_processResponse(
113+
$this->http->post(
114+
$path,
115+
$headers,
116+
$body
117+
)
118+
);
119+
}
120+
121+
/**
122+
* PUT to the resource at the specified path.
123+
*
124+
* @param string $path Path to the resource
125+
* @param array $params Query string parameters
126+
*
127+
* @return object The object representation of the resource
128+
*/
129+
public function updateData($path, $body = "")
130+
{
131+
$path = "/api/$this->version/$path";
132+
$headers = array('Content-Type' => 'application/json');
133+
return empty($params)
134+
? $this->_processResponse($this->http->put($path, $headers))
135+
: $this->_processResponse(
136+
$this->http->put(
137+
$path,
138+
$headers,
139+
$body
140+
)
141+
);
142+
}
143+
144+
private function _processResponse($response)
145+
{
146+
list($status, $headers, $body) = $response;
147+
if ($status == 204) {
148+
return TRUE;
149+
}
150+
if (empty($headers['Content-Type'])) {
151+
throw new Services_Zencoder_Exception('Response header is missing Content-Type');
152+
}
153+
switch ($headers['Content-Type']) {
154+
case 'application/json':
155+
case 'application/json; charset=utf-8':
156+
return $this->_processJsonResponse($status, $headers, $body);
157+
break;
158+
}
159+
throw new Services_Zencoder_Exception(
160+
'Unexpected content type: ' . $headers['Content-Type']);
161+
}
162+
163+
private function _processJsonResponse($status, $headers, $body) {
164+
$decoded = json_decode($body);
165+
if ($status >= 200 && $status < 300) {
166+
return $decoded;
167+
}
168+
throw new Services_Zencoder_Exception(
169+
(int)$decoded->status,
170+
$decoded->message,
171+
isset($decoded->code) ? $decoded->code : null,
172+
isset($decoded->more_info) ? $decoded->more_info : null
173+
);
174+
}
175+
}

Services/Zencoder/Accounts.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*
3+
4+
Zencoder API PHP Library
5+
Version: 2.0
6+
See the README file for info on how to use this library.
7+
8+
*/
9+
10+
class Services_Zencoder_Accounts extends Services_Zencoder_Base {
11+
public function create($params = NULL) {
12+
if(is_string($params)) {
13+
$json = trim($params);
14+
} else if(is_array($params)) {
15+
$json = json_encode($params);
16+
} else {
17+
throw new Services_Zencoder_Exception(
18+
'Account parameters required to create account.');
19+
}
20+
return $this->proxy->createData("account", $json);
21+
}
22+
23+
public function details() {
24+
return $this->proxy->retrieveData("account");
25+
}
26+
27+
public function integration() {
28+
return $this->proxy->updateData("account/integration");
29+
}
30+
31+
public function live() {
32+
return $this->proxy->updateData("account/live");
33+
}
34+
}

Services/Zencoder/Base.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
4+
Zencoder API PHP Library
5+
Version: 2.0
6+
See the README file for info on how to use this library.
7+
8+
*/
9+
10+
abstract class Services_Zencoder_Base
11+
implements Services_Zencoder_HttpProxy {
12+
13+
protected $proxy;
14+
15+
public function __construct(Services_Zencoder_HttpProxy $proxy)
16+
{
17+
$this->proxy = $proxy;
18+
}
19+
20+
public function createData($path, $body = "")
21+
{
22+
return $this->proxy->createData($path, $params);
23+
}
24+
25+
public function retrieveData($path, array $params = array())
26+
{
27+
return $this->proxy->retrieveData($path, $params);
28+
}
29+
30+
public function updateData($path, $body = "")
31+
{
32+
return $this->proxy->updateData($path, $params);
33+
}
34+
35+
public function deleteData($path)
36+
{
37+
return $this->proxy->deleteData($path, $params);
38+
}
39+
}

0 commit comments

Comments
 (0)