-
Notifications
You must be signed in to change notification settings - Fork 1
/
Antideo.class.php
185 lines (157 loc) · 4.64 KB
/
Antideo.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
<?php
class Antideo {
/**
* Constructor
*
* @param string $apiKey <b>Optional</b> needs to be set if have API Key
*/
function __construct($apiKey = null) {
$this->apiKey = $apiKey;
}
/**
* Queries an IP address health
*
* @param string $email a valid email address to be queried
*
* @return stdClass email address result object
*
* @throws <b>Exception</b> if an error occurs while executing this request
*/
public function email($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
try {
$result = $this->call('/email/'.$email);
return $result;
} catch (Exception $e) {
throw $e;
}
} else {
throw new Exception($email.' is not a valid email address', 0);
}
}
/**
* Queries an IP address health
*
* @param string $ip a valid IP address to be queried
*
* @return stdClass ip address health object
*
* @throws <b>Exception</b> if an error occurs while executing this request
*/
public function ipHealth($ip) {
try {
$result = $this->ipCall('/ip/health/',$ip);
return $result;
} catch (Exception $e) {
throw $e;
}
}
/**
* Queries an IP address location
*
* @param string $ip a valid IP address to be queried
*
* @return stdClass ip address location object
*
* @throws <b>Exception</b> if an error occurs while executing this request
*/
public function ipLocation($ip) {
try {
$result = $this->ipCall('/ip/location/',$ip);
return $result;
} catch (Exception $e) {
throw $e;
}
}
/**
* Queries an IP address info
*
* @param string $ip a valid IP address to be queried
*
* @return stdClass ip address info object
*
* @throws <b>Exception</b> if an error occurs while executing this request
*/
public function ipInfo($ip) {
try {
$result = $this->ipCall('/ip/info/',$ip);
return $result;
} catch (Exception $e) {
throw $e;
}
}
/**
* This will hold apiKey if passed in the constructor
* @var string
*/
private $apiKey;
/**
* Base URL to Antideo's REST API
* @var string
*/
private $baseURL = 'https://api.antideo.com';
/**
* Private helper method executing the HTTP request
*
* @param string $path a URL path to be appended to baseURL
*
* @return stdClass response object returned from Antideo
*
* @throws <b>Exception</b> if an error occurs while executing this request
*/
private function call($path) {
$ch = curl_init($this->baseURL . $path);
if(isset($this->apiKey)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'apiKey:'.$this->apiKey
));
}
curl_setopt($ch,CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$httpcode = $info['http_code'];
curl_close($ch);
if($httpcode == 200) {
$r = json_decode($result);
return $r;
} else {
throw $this->makeException($httpcode);
}
}
/**
* Helper method to execute API call if IP address is valid,
* or thrown an exception otherwise
*
* @param string $path a URL path to be appended to baseURL
* @param string $ip IP address candidate to be queried
*
* @return stdClass ip address health object
*
* @throws <b>Exception</b> if an error occurs
*/
private function ipCall($path, $ip) {
if(filter_var($ip, FILTER_VALIDATE_IP)) {
try {
$result = $this->call($path . $ip);
return $result;
} catch (Exception $e) {
throw $e;
}
} else {
throw new Exception($ip.' is not a valid IP address', 0);
}
}
/**
* Helper method to create HTTP response exception
*
* @param integer $code a HTTP response code returned
*
* @return exception Exception containing http response code and http response description
*/
private function makeException($code) {
$description = [400=>'Bad Request',401=>'Unauthorized',404=>'Method Not Allowed',429=>'Too Many Requests',500=>'Internal Server Error',503=>'Service Unavailable'];
return new Exception($description[$code], $code);
}
}
?>