-
Notifications
You must be signed in to change notification settings - Fork 62
/
tropo-rest.class.php
379 lines (310 loc) · 9.88 KB
/
tropo-rest.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* This file contains PHP classes that can be used to interact with the Tropo REST API/
* @see https://www.tropo.com
*
* @copyright 2010 Mark J. Headd (http://www.voiceingov.org)
* @package TropoPHP
* @author Mark Headd
*/
class SessionAPI extends RestBase {
// URL for the Tropo session API.
const SessionURL = 'http://api.tropo.com/1.0/sessions?action=create&token=';
// Success response from Tropo Session API.
const SessionResponse = '<success>true</success>';
public function __construct() {
parent::__construct();
}
/**
* Launch a new Tropo session.
*
* @param string $token
* @param array $params
* @return boolean
*/
public function createSession($token, Array $params = null) {
if(isset($params)) {
foreach ($params as $key=>$value) {
@ $querystring .= '&'. $key . '=' . $value;
}
}
curl_setopt($this->ch, CURLOPT_URL, self::SessionURL.$token.$querystring);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($this->ch);
$error = curl_error($this->ch);
parent::__destruct();
if($result === false) {
throw new Exception('An error occurred: '.$error);
} else {
if (strpos($result, self::SessionResponse) === false) {
throw new Exception('An error occurred: Tropo session launch failed.');
}
return true;
}
}
}
class ProvisioningAPI extends RestBase {
// URL for the Tropo provisioning API.
const ProvisioningURLBase = 'http://api.tropo.com/provisioning/';
public function __construct($userid, $password) {
parent::__construct($userid, $password);
}
/**
* Create a new Tropo application.
*
* @param string $href
* @param string $name
* @param string $voiceUrl
* @param string $messagingUrl
* @param string $platform
* @param string $partition
* @return string JSON
*/
public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition) {
$payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition));
$url = self::ProvisioningURLBase.'applications';
return self::makeAPICall('POST', $url, $payload);
}
/**
* Update an existing Tropo application to add an address.
*
* @param string $applicationID
* @param string $type
* @param string $prefix
* @param string $number
* @param string $city
* @param string $state
* @param string $channel
* @param string $username
* @param string $password
* @param string $token
* @return string JSON
*/
public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) {
$payload = json_encode(new Address($type, $prefix, $number, $city, $state, $channel, $username, $password, $token));
$url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses';
return self::makeAPICall('POST', $url, $payload);
}
/**
* Update an application property.
*
* @param string $applicationID
* @param string $href
* @param string $name
* @param string $voiceUrl
* @param string $messagingUrl
* @param string $platform
* @param string $partition
* @return string JSON
*/
public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) {
$payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition));
$url = self::ProvisioningURLBase.'applications/'.$applicationID;
return self::makeAPICall('PUT', $url, $payload);
}
/**
* Delete an existing Tropo application.
*
* @param string $applicationID
* @return string JSON
*/
public function deleteApplication($applicationID) {
$url = self::ProvisioningURLBase.'applications/'.$applicationID;
return self::makeAPICall('DELETE', $url);
}
/**
* Delete an application address.
*
* @param string $applicationID
* @param string $type
* @param string $address
* @return string JSON
*/
public function deleteApplicationAddress($applicationID, $type, $address) {
$url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses/'.$type.'/'.$address;
return self::makeAPICall('DELETE', $url);
}
/**
* View all applications for an account.
*
* @return string JSON
*/
public function viewApplications() {
$url = self::ProvisioningURLBase.'applications';
return self::makeAPICall('GET', $url);
}
/**
* View the details of a specific application.
*
* @param string $applicationID
* @return string JSON
*/
public function viewSpecificApplication($applicationID) {
$url = self::ProvisioningURLBase.'applications/'.$applicationID;
return self::makeAPICall('GET', $url);
}
/**
* View all of the addreses for an application.
*
* @param string $applicationID
* @return string JSON
*/
public function viewAddresses($applicationID) {
$url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses';
return self::makeAPICall('GET', $url);
}
/**
* View a list of availalbe exchanges
*
* @return string JSON
*/
public function viewExchanges() {
$url = self::ProvisioningURLBase.'exchanges';
return self::makeAPICall('GET', $url);
}
/**
* Method to make REST API call.
*
* @param string $method
* @param string $url
* @param string $payload
* @return string JSON
*/
private function makeAPICall($method, $url, $payload=NULL) {
if(($method == 'POST' || $method == 'PUT') && !isset($payload)) {
throw new Exception("Method $method requires payload for request body.");
}
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
switch($method) {
case 'POST':
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($payload)));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload);
break;
case 'PUT':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($payload)));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload);
break;
case 'DELETE':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
default:
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
}
$this->result = curl_exec($this->ch);
$this->error = curl_error($this->ch);
$this->curl_info = curl_getinfo($this->ch);
$this->curl_http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
if($this->result === false) {
throw new Exception('An error occurred: '.$this->error);
} else {
if ($this->curl_http_code != '200') {
throw new Exception('An error occurred: Invalid HTTP response returned: '.$this->curl_http_code);
}
return $this->result;
}
}
public function getResult() {
return $this->result;
}
public function getInfo() {
return $this->curl_info;
}
public function getHTTPCode() {
return $this->curl_http_code;
}
public function __destruct() {
parent::__destruct();
}
}
/**
* Base class for all REST classes.
*
*/
class RestBase {
protected $ch;
protected $result;
protected $error;
protected $curl_info;
protected $curl_http_code;
public function __construct($userid=NULL, $password=NULL) {
if (!function_exists('curl_init')) {
throw new Exception('PHP curl not installed.');
}
$this->ch = curl_init();
if(isset($userid) && isset($password)) {
curl_setopt($this->ch, CURLOPT_USERPWD, "$userid:$password");
}
}
public function __destruct() {
@ curl_close($this->ch);
}
}
/**
* Application class. Represents a Tropo application.
*
*/
class Application {
public function __construct($href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) {
if(isset($href)) { $this->href = $href; }
if(isset($name)) { $this->name = $name; }
if(isset($voiceUrl)) { $this->voiceUrl = $voiceUrl; }
if(isset($messagingUrl)) { $this->messagingUrl = $messagingUrl; }
if(isset($platform)) { $this->platform = $platform; }
if(isset($partition)) { $this->partition = $partition; }
}
public function __set($attribute, $value) {
$this->$attribute= $value;
}
}
/**
* Address class. Represents an address assigned to a Tropo application.
*
*/
class Address {
public function __construct($type=NULL, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) {
if(isset($type)) { $this->type = $type; }
if(isset($prefix)) { $this->prefix = $prefix; }
if(isset($number)) { $this->number = $number; }
if(isset($city)) { $this->type = $type; }
if(isset($state)) { $this->state = $state; }
if(isset($channel)) { $this->channel = $channel; }
if(isset($username)) { $this->username = $username; }
if(isset($password)) { $this->password = $password; }
if(isset($token)) { $this->token = $token; }
}
public function __set($attribute, $value) {
$this->$attribute= $value;
}
}
/**
* Exchange class. Represents an exchange.
*
*/
class Exchange {
public function __construct($prefix=NULL, $city=NULL, $state=NULL, $country=NULL) {
if(isset($prefix)) { $this->prefix = $prefix; }
if(isset($city)) { $this->city = $city; }
if(isset($state)) { $this->state = $state; }
if(isset($country)) { $this->country = $country; }
}
public function __set($attribute, $value) {
$this->$attribute= $value;
}
}
/**
* Helper class listing the type of addresses available to use with Tropo applications.
*
*/
class AddressType {
public static $number = "number";
public static $token = "token";
public static $aim = "aim";
public static $gtalk = "gtalk";
public static $jabber = "jabber";
public static $msn = "msn";
public static $yahoo = "yahoo";
public static $skype = "skype";
}
?>