-
Notifications
You must be signed in to change notification settings - Fork 1
/
everyoneapi.php
117 lines (104 loc) · 3.47 KB
/
everyoneapi.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
<?php
/**
* everyoneAPI PHP Library
*
* USAGE:
*
* #create everyoneAPI object.
* $obj = new everyoneAPI("[account_sid]", "[auth_token]");
*
* #get all data points.
* $data = $obj->getData("5551234567");
*
* #get one data point.
* $data = $obj->getData("5551234567", "cnam");
*
* #get multiple data points.
* $data = $obj->getData("5551234567", "cnam,line_provider,linetype");
*
*
* NOTES:
*
* The datapoints parameter in the getData method is optional. If you omit you will receive all the
* data points. If you do include it make sure the data point identifiers are comma separated,
* with no spaces and no trailing comma. You will receive a 400 (Bad Request) response from the
* API if this parameter is not formatted correctly.
*
* You can view a list of data point identifiers in the everyoneAPI docs.
* https://www.everyoneapi.com/docs
*
*
* @author Antonio Pita <[email protected]>
* @copyright 2016 CTiTelecom Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
class everyoneAPI
{
private $account_sid;
private $auth_token;
private $request;
public function __construct($account_sid, $auth_token)
{
$this->account_sid = $account_sid;
$this->auth_token = $auth_token;
}
public function getData($phone, $datapoints = NULL)
{
// Create the request.
$request = "https://api.everyoneapi.com/v1/phone/" . urlencode($phone) . "?";
if($datapoints != NULL){
$request .= "data=" . $datapoints . "&";
}
$request .= "account_sid=" . urlencode($this->account_sid) . "&auth_token=" . urlencode($this->auth_token);
// Init CURL session.
$session = curl_init($request);
// Set CURL options.
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Send the request.
$response = curl_exec($session);
// Close CURL session.
curl_close($session);
// Get HTTP status code from the response.
$httpstatus = array();
preg_match('/\d\d\d/', $response, $httpstatus);
// Check the HTTP status code for errors and return error if exists.
switch ($httpstatus[0]) {
case 200:
// Request was successful. Move on.
break;
case 400:
$error = "400 Bad Request";
return $error;
break;
case 401:
$error = "401 Unauthorized";
return $error;
break;
case 402:
$error = "402 Payment Required";
return $error;
break;
case 403:
$error = "403 Forbidden";
return $error;
break;
case 404:
$error = "404 Not Found";
return $error;
break;
case 503:
$error = "503 Service Unavailable";
return $error;
default:
$error = "Unknown Error: HTTP Status Code (" . $httpstatus[0] . ")";
return $error;
}
// Split JSON from headers.
$json = substr($response, strpos($response, "{"));
// Return result as an array.
$array = json_decode($json);
return $array;
}
}
?>