-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfonenode.php
More file actions
363 lines (298 loc) · 9.21 KB
/
fonenode.php
File metadata and controls
363 lines (298 loc) · 9.21 KB
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
<?php
/*
* PHP wrapper for the Fonenode API (fonenode.com/docs)
*
* https://github.com/fonenode/php_wrapper
*
* @author Opeyemi Obembe (@kehers) <ope@fonenode.com>
* @version 0.9.3
*/
require 'exceptions.php';
class fonenode {
private $_error;
private $_status;
private $_auth_id;
private $_auth_secret;
public $_api_root = "https://api.fonenode.com/v1/";
/*
* Constructor
*/
public function __construct($id, $secret) {
$this->_auth_id = $id;
$this->_auth_secret = $secret;
}
/*
* Helpers
*/
/*
* Get last status code
*/
public function getStatusCode() {
return $this->_status;
}
/*
* Get last error
*/
public function getError() {
return $this->_error;
}
/*
* Responses
* fonenode.com/docs#responses
*/
/*
* Create Response
* Create a response you can later attach to a call or number.
* fonenode.com/docs#responses-create
* param: array of parameters as detailed in doc
* e.g array('text'=>'holla',voice='man');
*/
public function create_response($data) {
$url = 'responses';
$response = $this->post($url, $data);
return json_decode($response, true);
}
/*
* List Responses
* List responses you have created.
* fonenode.com/docs#responses-list
*/
public function list_responses($limit = 20, $offset = 0) {
$url = 'responses?';
if ((int) $limit)
$url .= 'limit='.$limit;
if ((int) $limit && (int) $offset)
$url .= '&';
if ((int) $offset)
$url .= 'offset='.$offset;
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Get a response
* fonenode.com/docs#responses-get
*/
public function get_response($id) {
if (!$id)
throw new MissingParameterException("Response id missing.");
$url = 'responses/'.$id;
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Delete a response
* returns true or false
* fonenode.com/docs#responses-delete
*/
public function delete_response($id) {
if (!$id)
throw new MissingParameterException("Response id missing.");
$url = 'responses/'.$id;
$response = $this->delete($url);
$json = json_decode($response, true);
if ($json['id']) {
return true;
}
else {
$this->_error = $json['error'];
return false;
}
}
/*
* Calls
* fonenode.com/docs#calls
*/
/*
* Quick call
* Make a quick call without creating a response
* fonenode.com/docs#calls-quick
*/
public function quick_call($to, $text, $voice = null, $from = null) {
$url = 'calls/quick';
if (!$to)
throw new MissingParameterException("To parameter missing.");
if (!$text)
throw new MissingParameterException("Call text/link missing.");
$data = array(
'to' => $to,
'text' => $text
);
if ($voice)
$data['voice'] = $voice;
if ($from)
$data['from'] = $from;
$response = $this->post($url, $data);
return json_decode($response, true);
}
/*
* Call
* Make calls to one or more numbers using a response already created.
* fonenode.com/docs#calls-make
*/
public function call($to, $response_id, $voice = null, $from = null) {
$url = 'calls';
if (!$to)
throw new MissingParameterException("To parameter missing.");
if (!$response_id)
throw new MissingParameterException("Response id missing.");
$data = array(
'to' => $to,
'response_id' => $response_id
);
if ($from)
$data['from'] = $from;
if ($voice)
$data['voice'] = $voice;
$response = $this->post($url, $data);
return json_decode($response, true);
}
/*
* List Calls
* List inbound and outbound calls via your account.
* $filter: picked, notpicked, called, notcalled, outbound or inbound
* fonenode.com/docs#calls-list
*/
public function list_calls($limit = 20, $offset = 0, $filter = null) {
$url = 'calls?';
if ((int) $limit)
$query['limit'] = $limit;
if ((int) $offset)
$query['offset'] = $offset;
if ($filter)
$query['filter'] = $filter;
$url .= http_build_query($query);
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Get a call
* fonenode.com/docs#calls-get
*/
public function get_call($id) {
if (!$id)
throw new MissingParameterException("Call id missing.");
$url = 'calls/'.$id;
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Numbers
* fonenode.com/docs#numbers
*/
/*
* List My Numbers
* List returns numbers you own.
* fonenode.com/docs#numbers-my
*/
public function list_my_numbers($limit = 20, $offset = 0) {
$url = 'numbers?';
if ((int) $limit)
$query['limit'] = $limit;
if ((int) $offset)
$query['offset'] = $offset;
$url .= http_build_query($query);
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Update Number response
* Attach a response to a number.
* fonenode.com/docs#numbers-update
*/
public function update_number_response($number_id, $response_id) {
if (!$number_id)
throw new MissingParameterException("Number id missing.");
if (!$response_id)
throw new MissingParameterException("Response id missing.");
$url = 'numbers/'.$number_id;
$response = $this->put($url, array('response_id' => $response_id));
return json_decode($response, true);
}
/*
* Billing
* fonenode.com/docs#billing
*/
/*
* List billing reports
* $filter: numbers, outbound or inbound
* fonenode.com/docs#bills-list
*/
public function list_bills($limit = 20, $offset = 0, $filter = null) {
$url = 'billing?';
if ((int) $limit)
$query['limit'] = $limit;
if ((int) $offset)
$query['offset'] = $offset;
if ($filter)
$query['filter'] = $filter;
$url .= http_build_query($query);
$response = $this->get($url);
return json_decode($response, true);
}
/*
* Private methods
*/
private function _build_post_query($data, $prefix = null) {
$query = array();
foreach ($data as $key => $value) {
$k = isset($prefix) ? $prefix . '[' . $key . ']' : $key;
if (is_array($value)) {
$query += $this->_build_post_query($value, $k);
} else {
$query[$k] = $value;
}
}
return $query;
}
private function build_post_query($data) {
$arr = $this->_build_post_query($data);
foreach ($arr as $k => $v) {
$query .= $k.'='.urlencode($v).'&';
}
return substr($query, 0, -1);
}
private function get($url) {
return $this->http($url);
}
private function post($url, $data) {
$query = $this->build_post_query($data);
return $this->http($url, $query);
}
private function put($url, $data) {
return $this->http($url, $data, 'PUT');
}
private function delete($url) {
// Instead of using curl's CURLOPT_CUSTOMREQUEST,
// we use the get hack for now
return $this->http($url.'?_method=delete');
}
/**
* HTTP request handler
*
* $url: url to post or get
* $data: post data for POST
* @returns array of http status and response
*/
private function http($url, $data = null, $custom_request = 'POST') {
// print_r($data); #debug
$ch = curl_init();
$callurl = $this->_api_root.$url;
// echo $callurl; #debug
curl_setopt($ch, CURLOPT_URL, $callurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set to 0 to not verify ssl
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/cert_bundle.crt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->_auth_id.':'.$this->_auth_secret);
if (isset($data)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $custom_request);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
$this->_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//echo $response; #debug
return $response;
}
}
?>