forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwilio.php
313 lines (294 loc) · 10.9 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
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
<?php
/*
* Author: Neuman Vong neuman@twilio.com
* License: http://creativecommons.org/licenses/MIT/ MIT
* Link: https://twilio-php.readthedocs.org/en/latest/
*/
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');
/**
* Create a client to talk to the Twilio API.
*
*
* :param string $sid: Your Account SID
* :param string $token: Your Auth Token from `your dashboard
* <https://www.twilio.com/user/account>`_
* :param string $version: API version to use
* :param $_http: A HTTP client for making requests.
* :type $_http: :php:class:`Services_Twilio_TinyHttp`
* :param int $retryAttempts:
* Number of times to retry failed requests. Currently only idempotent
* requests (GET's and DELETE's) are retried.
*
* Here's an example:
*
* .. code-block:: php
*
* require('Services/Twilio.php');
* $client = new Services_Twilio('AC123', '456bef', null, null, 3);
* // Take some action with the client, etc.
*/
class Services_Twilio extends Services_Twilio_Resource
{
const USER_AGENT = 'twilio-php/3.12.4';
protected $http;
protected $retryAttempts;
protected $last_response;
protected $version;
protected $versions = array('2008-08-01', '2010-04-01');
public function __construct(
$sid,
$token,
$version = null,
Services_Twilio_TinyHttp $_http = null,
$retryAttempts = 1
) {
$this->version = in_array($version, $this->versions) ?
$version : end($this->versions);
if (null === $_http) {
if (!in_array('openssl', get_loaded_extensions())) {
throw new Services_Twilio_HttpException("The OpenSSL extension is required but not currently enabled. For more information, see http://php.net/manual/en/book.openssl.php");
}
if (in_array('curl', get_loaded_extensions())) {
$_http = new Services_Twilio_TinyHttp(
"https://api.twilio.com",
array(
"curlopts" => array(
CURLOPT_USERAGENT => self::qualifiedUserAgent(phpversion()),
CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
),
)
);
} else {
$_http = new Services_Twilio_HttpStream(
"https://api.twilio.com",
array(
"http_options" => array(
"http" => array(
"user_agent" => self::qualifiedUserAgent(phpversion()),
"header" => "Accept-Charset: utf-8\r\n",
),
"ssl" => array(
'verify_peer' => true,
'cafile' => dirname(__FILE__) . '/cacert.pem',
'verify_depth' => 5,
),
),
)
);
}
}
$_http->authenticate($sid, $token);
$this->http = $_http;
$this->accounts = new Services_Twilio_Rest_Accounts($this, "/{$this->version}/Accounts");
$this->account = $this->accounts->get($sid);
$this->retryAttempts = $retryAttempts;
}
/**
* Fully qualified user agent with the current PHP Version.
*
* :return: the user agent
* :rtype: string
*/
public static function qualifiedUserAgent($php_version) {
return self::USER_AGENT . " (php $php_version)";
}
/**
* Get the api version used by the rest client
*
* :return: the API version in use
* :returntype: string
*/
public function getVersion() {
return $this->version;
}
/**
* Get the retry attempt limit used by the rest client
*
* :return: the number of retry attempts
* :rtype: int
*/
public function getRetryAttempts() {
return $this->retryAttempts;
}
/**
* Construct a URI based on initial path, query params, and paging
* information
*
* We want to use the query params, unless we have a next_page_uri from the
* API.
*
* :param string $path: The request path (may contain query params if it's
* a next_page_uri)
* :param array $params: Query parameters to use with the request
* :param boolean $full_uri: Whether the $path contains the full uri
*
* :return: the URI that should be requested by the library
* :returntype: string
*/
public static function getRequestUri($path, $params, $full_uri = false) {
$json_path = $full_uri ? $path : "$path.json";
if (!$full_uri && !empty($params)) {
$query_path = $json_path . '?' . http_build_query($params, '', '&');
} else {
$query_path = $json_path;
}
return $query_path;
}
/**
* Helper method for implementing request retry logic
*
* :param array $callable: The function that makes an HTTP request
* :param string $uri: The URI to request
* :param int $retriesLeft: Number of times to retry
*
* :return: The object representation of the resource
* :rtype: object
*/
protected function _makeIdempotentRequest($callable, $uri, $retriesLeft) {
$response = call_user_func_array($callable, array($uri));
list($status, $headers, $body) = $response;
if ($status >= 500 && $retriesLeft > 0) {
return $this->_makeIdempotentRequest($callable, $uri, $retriesLeft - 1);
} else {
return $this->_processResponse($response);
}
}
/**
* GET the resource at the specified path.
*
* :param string $path: Path to the resource
* :param array $params: Query string parameters
* :param boolean $full_uri: Whether the full URI has been passed as an
* argument
*
* :return: The object representation of the resource
* :rtype: object
*/
public function retrieveData($path, $params = array(),
$full_uri = false
) {
$uri = self::getRequestUri($path, $params, $full_uri);
return $this->_makeIdempotentRequest(array($this->http, 'get'),
$uri, $this->retryAttempts);
}
/**
* DELETE the resource at the specified path.
*
* :param string $path: Path to the resource
* :param array $params: Query string parameters
*
* :return: The object representation of the resource
* :rtype: object
*/
public function deleteData($path, $params = array())
{
$uri = self::getRequestUri($path, $params);
return $this->_makeIdempotentRequest(array($this->http, 'delete'),
$uri, $this->retryAttempts);
}
/**
* POST to the resource at the specified path.
*
* :param string $path: Path to the resource
* :param array $params: Query string parameters
*
* :return: The object representation of the resource
* :rtype: object
*/
public function createData($path, $params = array())
{
$path = "$path.json";
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
$response = $this->http->post(
$path, $headers, self::buildQuery($params, '')
);
return $this->_processResponse($response);
}
/**
* Build a query string from query data
*
* :param array $queryData: An associative array of keys and values. The
* values can be a simple type or a list, in which case the list is
* converted to multiple query parameters with the same key.
* :param string $numericPrefix:
* :param string $queryStringStyle: Determine how to build the url
* - strict: Build a standards compliant query string without braces (can be hacked by using braces in key)
* - php: Build a PHP compatible query string with nested array syntax
* :return: The encoded query string
* :rtype: string
*/
public static function buildQuery($queryData, $numericPrefix = '') {
$query = '';
// Loop through all of the $query_data
foreach ($queryData as $key => $value) {
// If the key is an int, add the numeric_prefix to the beginning
if (is_int($key)) {
$key = $numericPrefix . $key;
}
// If the value is an array, we will end up recursing
if (is_array($value)) {
// Loop through the values
foreach ($value as $value2) {
// Add an arg_separator if needed
if ($query !== '') {
$query .= '&';
}
// Recurse
$query .= self::buildQuery(array($key => $value2), $numericPrefix);
}
} else {
// Add an arg_separator if needed
if ($query !== '') {
$query .= '&';
}
// Add the key and the urlencoded value (as a string)
$query .= $key . '=' . urlencode((string)$value);
}
}
return $query;
}
/**
* Convert the JSON encoded resource into a PHP object.
*
* :param array $response: 3-tuple containing status, headers, and body
*
* :return: PHP object decoded from JSON
* :rtype: object
* :throws: A :php:class:`Services_Twilio_RestException` if the Response is
* in the 300-500 range of status codes.
*/
private function _processResponse($response)
{
list($status, $headers, $body) = $response;
if ($status === 204) {
return true;
}
$decoded = json_decode($body);
if ($decoded === null) {
throw new Services_Twilio_RestException(
$status,
'Could not decode response body as JSON. ' .
'This likely indicates a 500 server error'
);
}
if (200 <= $status && $status < 300) {
$this->last_response = $decoded;
return $decoded;
}
throw new Services_Twilio_RestException(
$status,
isset($decoded->message) ? $decoded->message : '',
isset($decoded->code) ? $decoded->code : null,
isset($decoded->more_info) ? $decoded->more_info : null
);
}
}