forked from clintberry/twilio2plivo-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twilio.php
executable file
·166 lines (155 loc) · 5.19 KB
/
Twilio.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
<?php
function Services_Twilio_autoload($className) {
if (substr($className, 0, 15) != 'Services_Twilio') {
return false;
}
$file = str_replace('_', '/', $className);
$file = str_replace('Services/', '', $file);
return include dirname(__FILE__) . "/$file.php";
}
spl_autoload_register('Services_Twilio_autoload');
/**
* Twilio API client interface.
*
* @category Services
* @package Services_Twilio
* @author Neuman Vong <[email protected]>
* @license http://creativecommons.org/licenses/MIT/ MIT
* @link http://pear.php.net/package/Services_Twilio
*/
class Services_Twilio extends Services_Twilio_Resource
{
const USER_AGENT = 'plivo-php/0.1.0';
protected $http;
protected $version;
/**
* Constructor.
*
* @param string $sid Account SID
* @param string $token Account auth token
* @param string $version API version
* @param Services_Twilio_Http $_http A HTTP client
*/
public function __construct(
$sid,
$token,
$version = 'v1',
Services_Twilio_TinyHttp $_http = null
) {
$this->version = $version;
if (null === $_http) {
$_http = new Services_Twilio_TinyHttp(
"https://api.plivo.com",
array("curlopts" => array(CURLOPT_USERAGENT => self::USER_AGENT))
);
}
$_http->authenticate($sid, $token);
$this->http = $_http;
$this->accounts = new Services_Twilio_Rest_Accounts($this);
$this->account = $this->accounts->get($sid);
}
/**
* GET the resource at the specified path.
*
* @param string $path Path to the resource
* @param array $params Query string parameters
*
* @return object The object representation of the resource
*/
public function retrieveData($path, array $params = array())
{
$path = "/$this->version/$path";
return empty($params)
? $this->_processResponse($this->http->get($path))
: $this->_processResponse(
$this->http->get("$path?" . http_build_query($params, '', '&'))
);
}
/**
* DELETE the resource at the specified path.
*
* @param string $path Path to the resource
* @param array $params Query string parameters
*
* @return object The object representation of the resource
*/
public function deleteData($path, array $params = array())
{
$path = "/$this->version/$path";
return empty($params)
? $this->_processResponse($this->http->delete($path))
: $this->_processResponse(
$this->http->delete("$path?" . http_build_query($params, '', '&'))
);
}
/**
* POST to the resource at the specified path.
*
* @param string $path Path to the resource
* @param array $params Query string parameters
*
* @return object The object representation of the resource
*/
public function createData($path, array $params = array())
{
$path = "/$this->version/$path";
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
return empty($params)
? $this->_processResponse($this->http->post($path, $headers))
: $this->_processResponse(
$this->http->post(
$path,
$headers,
http_build_query($params, '', '&')
)
);
}
/**
* Convert the JSON encoded resource into a PHP object.
*
* @param array $response 3-tuple containing status, headers, and body
*
* @return object PHP object decoded from JSON
*/
private function _processResponse($response)
{
list($status, $headers, $body) = $response;
if ($status == 204) {
return TRUE;
}
if (empty($headers['Content-Type'])) {
throw new DomainException('Response header is missing Content-Type');
}
switch ($headers['Content-Type']) {
case 'application/json':
return $this->_processJsonResponse($status, $headers, $body);
break;
case 'text/xml':
return $this->_processXmlResponse($status, $headers, $body);
break;
}
throw new DomainException(
'Unexpected content type: ' . $headers['Content-Type']);
}
private function _processJsonResponse($status, $headers, $body) {
$decoded = json_decode($body);
if (200 <= $status && $status < 300) {
return $decoded;
}
throw new Services_Twilio_RestException(
(int)$decoded->status,
$decoded->message,
isset($decoded->code) ? $decoded->code : null,
isset($decoded->more_info) ? $decoded->more_info : null
);
}
private function _processXmlResponse($status, $headers, $body) {
$decoded = simplexml_load_string($body);
throw new Services_Twilio_RestException(
(int)$decoded->Status,
(string)$decoded->Message,
(string)$decoded->Code,
(string)$decoded->MoreInfo
);
}
}