@@ -12,290 +12,290 @@ |
||
12 | 12 | */ |
13 | 13 | class Ipinfo |
14 | 14 | { |
15 | - /** |
|
16 | - * The base url of the ipinfo service. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - const BASE_URL = 'https://ipinfo.io/'; |
|
21 | - |
|
22 | - /** |
|
23 | - * The ip string. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - const IP = 'ip'; |
|
28 | - |
|
29 | - /** |
|
30 | - * The hostname string. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - const HOSTNAME = 'hostname'; |
|
35 | - |
|
36 | - /** |
|
37 | - * The loc string. |
|
38 | - * |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - const LOC = 'loc'; |
|
42 | - |
|
43 | - /** |
|
44 | - * The org string. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - const ORG = 'org'; |
|
49 | - |
|
50 | - /** |
|
51 | - * The city string. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - const CITY = 'city'; |
|
56 | - |
|
57 | - /** |
|
58 | - * The region string. |
|
59 | - * |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - const REGION = 'region'; |
|
63 | - |
|
64 | - /** |
|
65 | - * The country string. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - const COUNTRY = 'country'; |
|
70 | - |
|
71 | - /** |
|
72 | - * The phone string. |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - const PHONE = 'phone'; |
|
77 | - |
|
78 | - /** |
|
79 | - * The geo string. |
|
80 | - * |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - const GEO = 'geo'; |
|
84 | - |
|
85 | - /** |
|
86 | - * The postal string. |
|
87 | - * |
|
88 | - * @var string |
|
89 | - */ |
|
90 | - const POSTAL = 'postal'; |
|
91 | - |
|
92 | - /** |
|
93 | - * All the settings. |
|
94 | - * |
|
95 | - * @var array |
|
96 | - */ |
|
97 | - protected $settings; |
|
98 | - |
|
99 | - /** |
|
100 | - * Create an Ipinfo instance. |
|
101 | - * |
|
102 | - * @param array $settings An array with all the settings. |
|
103 | - * Supported keys are: |
|
104 | - * - token: string the developer token; |
|
105 | - * - debug: boolean active or not the debug. |
|
106 | - */ |
|
107 | - public function __construct($settings = array()) |
|
108 | - { |
|
109 | - //Merge user settings |
|
110 | - $this->settings = array_merge(array( |
|
111 | - 'token' => '', |
|
112 | - 'connectionTimeout' => 0, |
|
113 | - 'timeout' => 0, |
|
114 | - 'debug' => false, |
|
115 | - ), $settings); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Get all the info about your own ip address. |
|
120 | - * |
|
121 | - * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
122 | - * @throws InvalidTokenException |
|
123 | - * @throws RateLimitExceedException |
|
124 | - */ |
|
125 | - public function getYourOwnIpDetails() |
|
126 | - { |
|
127 | - $response = $this->makeCurlRequest($this::BASE_URL.'json'); |
|
128 | - $response = $this->jsonDecodeResponse($response); |
|
129 | - |
|
130 | - return new Host($response); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Get all the info about an ip address. |
|
135 | - * |
|
136 | - * @param string $ipAddress The ip address. |
|
137 | - * |
|
138 | - * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
139 | - * @throws InvalidTokenException |
|
140 | - * @throws RateLimitExceedException |
|
141 | - */ |
|
142 | - public function getFullIpDetails($ipAddress) |
|
143 | - { |
|
144 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); |
|
145 | - $response = $this->jsonDecodeResponse($response); |
|
146 | - |
|
147 | - return new Host($response); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get a specific field value. |
|
152 | - * |
|
153 | - * @param string $ipAddress The ip address. |
|
154 | - * @param string $field The field. |
|
155 | - * |
|
156 | - * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
|
157 | - * This could returns an Host object if you call it with for the field |
|
158 | - * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
159 | - * @throws InvalidTokenException |
|
160 | - * @throws RateLimitExceedException |
|
161 | - */ |
|
162 | - public function getSpecificField($ipAddress, $field) |
|
163 | - { |
|
164 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); |
|
165 | - $response = $this->checkGeo($field, $response); |
|
166 | - |
|
167 | - return $response; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get a specific field value of your own ip address. |
|
172 | - * |
|
173 | - * @param string $field The field. |
|
174 | - * |
|
175 | - * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
|
176 | - * This could returns an Host object if you call it with for the field |
|
177 | - * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
178 | - * @throws InvalidTokenException |
|
179 | - * @throws RateLimitExceedException |
|
180 | - */ |
|
181 | - public function getYourOwnIpSpecificField($field) |
|
182 | - { |
|
183 | - $response = $this->makeCurlRequest($this::BASE_URL.$field); |
|
184 | - $response = $this->checkGeo($field, $response); |
|
185 | - |
|
186 | - return $response; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Use the /geo call to get just the geolocation information, which will often be |
|
191 | - * faster than getting the full response. |
|
192 | - * |
|
193 | - * @param string $ipAddress The ip address. |
|
194 | - * |
|
195 | - * @return \DavidePastore\Ipinfo\Host |
|
196 | - * @throws InvalidTokenException |
|
197 | - * @throws RateLimitExceedException |
|
198 | - */ |
|
199 | - public function getIpGeoDetails($ipAddress) |
|
200 | - { |
|
201 | - return $this->getSpecificField($ipAddress, $this::GEO); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Check if the response is GEO and set the parameters accordingly. |
|
206 | - * |
|
207 | - * @param string $field The field value. |
|
208 | - * @param string $response The response from the server. |
|
209 | - * |
|
210 | - * @return \DavidePastore\Ipinfo\Host|string Returns an Host object if the request is |
|
211 | - * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
|
212 | - * delete the last character ('\n'). |
|
213 | - * @throws InvalidTokenException |
|
214 | - * @throws RateLimitExceedException |
|
215 | - */ |
|
216 | - private function checkGeo($field, $response) |
|
217 | - { |
|
218 | - if ($field == $this::GEO) { |
|
219 | - $response = $this->jsonDecodeResponse($response); |
|
220 | - $response = new Host($response); |
|
221 | - } else { |
|
222 | - $this->checkErrors($response); |
|
223 | - $response = substr($response, 0, -1); |
|
224 | - } |
|
225 | - |
|
226 | - return $response; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Make a curl request. |
|
231 | - * |
|
232 | - * @param string $address The address of the request. |
|
233 | - * |
|
234 | - * @return string Returns the response from the request. |
|
235 | - */ |
|
236 | - private function makeCurlRequest($address) |
|
237 | - { |
|
238 | - $curl = curl_init(); |
|
239 | - |
|
240 | - if (!empty($this->settings['token'])) { |
|
241 | - $address .= '?token='.$this->settings['token']; |
|
242 | - } |
|
243 | - |
|
244 | - if ($this->settings['debug']) { |
|
245 | - echo 'Request address: '.$address."\n"; |
|
246 | - } |
|
247 | - |
|
248 | - curl_setopt_array($curl, array( |
|
249 | - CURLOPT_RETURNTRANSFER => 1, |
|
250 | - CURLOPT_URL => $address, |
|
251 | - CURLOPT_CONNECTTIMEOUT => $this->settings['connectionTimeout'], |
|
252 | - CURLOPT_TIMEOUT => $this->settings['timeout'], |
|
253 | - CURLOPT_CAINFO => __DIR__ . "/cacert.pem" |
|
254 | - )); |
|
255 | - |
|
256 | - $response = curl_exec($curl); |
|
257 | - |
|
258 | - if ($response === false && $this->settings['debug']) { |
|
259 | - $error = curl_error($curl); |
|
260 | - echo "The error is".$error; |
|
261 | - } |
|
262 | - |
|
263 | - curl_close($curl); |
|
264 | - |
|
265 | - return $response; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Returns the json decoded associative array. |
|
270 | - * @param string $response Response from the http call. |
|
271 | - * @return array Returns the associative array with the response. |
|
272 | - * @throws RateLimitExceedException If you exceed the rate limit. |
|
273 | - * @throws InvalidTokenException If the used token is invalid. |
|
274 | - */ |
|
275 | - private function jsonDecodeResponse($response) |
|
276 | - { |
|
277 | - if ($response) { |
|
278 | - // Check if the response contains an error message |
|
279 | - $this->checkErrors($response); |
|
280 | - $response = json_decode($response, true); |
|
281 | - } else { |
|
282 | - $response = array(); |
|
283 | - } |
|
284 | - return $response; |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Check if the given response has some kind of errors. |
|
289 | - * @param string $response The response to check. |
|
290 | - * @throws RateLimitExceedException If you exceed the rate limit. |
|
291 | - * @throws InvalidTokenException If the used token is invalid. |
|
292 | - */ |
|
293 | - private function checkErrors($response) |
|
294 | - { |
|
295 | - if (strpos($response, 'Rate limit exceeded.') !== false) { |
|
296 | - throw new RateLimitExceedException("You exceed the rate limit.", $response); |
|
297 | - } elseif (strpos($response, 'Unknown token.') !== false) { |
|
298 | - throw new InvalidTokenException("The used token is invalid.", $response); |
|
299 | - } |
|
300 | - } |
|
15 | + /** |
|
16 | + * The base url of the ipinfo service. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + const BASE_URL = 'https://ipinfo.io/'; |
|
21 | + |
|
22 | + /** |
|
23 | + * The ip string. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + const IP = 'ip'; |
|
28 | + |
|
29 | + /** |
|
30 | + * The hostname string. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + const HOSTNAME = 'hostname'; |
|
35 | + |
|
36 | + /** |
|
37 | + * The loc string. |
|
38 | + * |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + const LOC = 'loc'; |
|
42 | + |
|
43 | + /** |
|
44 | + * The org string. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + const ORG = 'org'; |
|
49 | + |
|
50 | + /** |
|
51 | + * The city string. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + const CITY = 'city'; |
|
56 | + |
|
57 | + /** |
|
58 | + * The region string. |
|
59 | + * |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + const REGION = 'region'; |
|
63 | + |
|
64 | + /** |
|
65 | + * The country string. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + const COUNTRY = 'country'; |
|
70 | + |
|
71 | + /** |
|
72 | + * The phone string. |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + const PHONE = 'phone'; |
|
77 | + |
|
78 | + /** |
|
79 | + * The geo string. |
|
80 | + * |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + const GEO = 'geo'; |
|
84 | + |
|
85 | + /** |
|
86 | + * The postal string. |
|
87 | + * |
|
88 | + * @var string |
|
89 | + */ |
|
90 | + const POSTAL = 'postal'; |
|
91 | + |
|
92 | + /** |
|
93 | + * All the settings. |
|
94 | + * |
|
95 | + * @var array |
|
96 | + */ |
|
97 | + protected $settings; |
|
98 | + |
|
99 | + /** |
|
100 | + * Create an Ipinfo instance. |
|
101 | + * |
|
102 | + * @param array $settings An array with all the settings. |
|
103 | + * Supported keys are: |
|
104 | + * - token: string the developer token; |
|
105 | + * - debug: boolean active or not the debug. |
|
106 | + */ |
|
107 | + public function __construct($settings = array()) |
|
108 | + { |
|
109 | + //Merge user settings |
|
110 | + $this->settings = array_merge(array( |
|
111 | + 'token' => '', |
|
112 | + 'connectionTimeout' => 0, |
|
113 | + 'timeout' => 0, |
|
114 | + 'debug' => false, |
|
115 | + ), $settings); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Get all the info about your own ip address. |
|
120 | + * |
|
121 | + * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
122 | + * @throws InvalidTokenException |
|
123 | + * @throws RateLimitExceedException |
|
124 | + */ |
|
125 | + public function getYourOwnIpDetails() |
|
126 | + { |
|
127 | + $response = $this->makeCurlRequest($this::BASE_URL.'json'); |
|
128 | + $response = $this->jsonDecodeResponse($response); |
|
129 | + |
|
130 | + return new Host($response); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Get all the info about an ip address. |
|
135 | + * |
|
136 | + * @param string $ipAddress The ip address. |
|
137 | + * |
|
138 | + * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
139 | + * @throws InvalidTokenException |
|
140 | + * @throws RateLimitExceedException |
|
141 | + */ |
|
142 | + public function getFullIpDetails($ipAddress) |
|
143 | + { |
|
144 | + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); |
|
145 | + $response = $this->jsonDecodeResponse($response); |
|
146 | + |
|
147 | + return new Host($response); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get a specific field value. |
|
152 | + * |
|
153 | + * @param string $ipAddress The ip address. |
|
154 | + * @param string $field The field. |
|
155 | + * |
|
156 | + * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
|
157 | + * This could returns an Host object if you call it with for the field |
|
158 | + * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
159 | + * @throws InvalidTokenException |
|
160 | + * @throws RateLimitExceedException |
|
161 | + */ |
|
162 | + public function getSpecificField($ipAddress, $field) |
|
163 | + { |
|
164 | + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); |
|
165 | + $response = $this->checkGeo($field, $response); |
|
166 | + |
|
167 | + return $response; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get a specific field value of your own ip address. |
|
172 | + * |
|
173 | + * @param string $field The field. |
|
174 | + * |
|
175 | + * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
|
176 | + * This could returns an Host object if you call it with for the field |
|
177 | + * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
178 | + * @throws InvalidTokenException |
|
179 | + * @throws RateLimitExceedException |
|
180 | + */ |
|
181 | + public function getYourOwnIpSpecificField($field) |
|
182 | + { |
|
183 | + $response = $this->makeCurlRequest($this::BASE_URL.$field); |
|
184 | + $response = $this->checkGeo($field, $response); |
|
185 | + |
|
186 | + return $response; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Use the /geo call to get just the geolocation information, which will often be |
|
191 | + * faster than getting the full response. |
|
192 | + * |
|
193 | + * @param string $ipAddress The ip address. |
|
194 | + * |
|
195 | + * @return \DavidePastore\Ipinfo\Host |
|
196 | + * @throws InvalidTokenException |
|
197 | + * @throws RateLimitExceedException |
|
198 | + */ |
|
199 | + public function getIpGeoDetails($ipAddress) |
|
200 | + { |
|
201 | + return $this->getSpecificField($ipAddress, $this::GEO); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Check if the response is GEO and set the parameters accordingly. |
|
206 | + * |
|
207 | + * @param string $field The field value. |
|
208 | + * @param string $response The response from the server. |
|
209 | + * |
|
210 | + * @return \DavidePastore\Ipinfo\Host|string Returns an Host object if the request is |
|
211 | + * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
|
212 | + * delete the last character ('\n'). |
|
213 | + * @throws InvalidTokenException |
|
214 | + * @throws RateLimitExceedException |
|
215 | + */ |
|
216 | + private function checkGeo($field, $response) |
|
217 | + { |
|
218 | + if ($field == $this::GEO) { |
|
219 | + $response = $this->jsonDecodeResponse($response); |
|
220 | + $response = new Host($response); |
|
221 | + } else { |
|
222 | + $this->checkErrors($response); |
|
223 | + $response = substr($response, 0, -1); |
|
224 | + } |
|
225 | + |
|
226 | + return $response; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Make a curl request. |
|
231 | + * |
|
232 | + * @param string $address The address of the request. |
|
233 | + * |
|
234 | + * @return string Returns the response from the request. |
|
235 | + */ |
|
236 | + private function makeCurlRequest($address) |
|
237 | + { |
|
238 | + $curl = curl_init(); |
|
239 | + |
|
240 | + if (!empty($this->settings['token'])) { |
|
241 | + $address .= '?token='.$this->settings['token']; |
|
242 | + } |
|
243 | + |
|
244 | + if ($this->settings['debug']) { |
|
245 | + echo 'Request address: '.$address."\n"; |
|
246 | + } |
|
247 | + |
|
248 | + curl_setopt_array($curl, array( |
|
249 | + CURLOPT_RETURNTRANSFER => 1, |
|
250 | + CURLOPT_URL => $address, |
|
251 | + CURLOPT_CONNECTTIMEOUT => $this->settings['connectionTimeout'], |
|
252 | + CURLOPT_TIMEOUT => $this->settings['timeout'], |
|
253 | + CURLOPT_CAINFO => __DIR__ . "/cacert.pem" |
|
254 | + )); |
|
255 | + |
|
256 | + $response = curl_exec($curl); |
|
257 | + |
|
258 | + if ($response === false && $this->settings['debug']) { |
|
259 | + $error = curl_error($curl); |
|
260 | + echo "The error is".$error; |
|
261 | + } |
|
262 | + |
|
263 | + curl_close($curl); |
|
264 | + |
|
265 | + return $response; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Returns the json decoded associative array. |
|
270 | + * @param string $response Response from the http call. |
|
271 | + * @return array Returns the associative array with the response. |
|
272 | + * @throws RateLimitExceedException If you exceed the rate limit. |
|
273 | + * @throws InvalidTokenException If the used token is invalid. |
|
274 | + */ |
|
275 | + private function jsonDecodeResponse($response) |
|
276 | + { |
|
277 | + if ($response) { |
|
278 | + // Check if the response contains an error message |
|
279 | + $this->checkErrors($response); |
|
280 | + $response = json_decode($response, true); |
|
281 | + } else { |
|
282 | + $response = array(); |
|
283 | + } |
|
284 | + return $response; |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Check if the given response has some kind of errors. |
|
289 | + * @param string $response The response to check. |
|
290 | + * @throws RateLimitExceedException If you exceed the rate limit. |
|
291 | + * @throws InvalidTokenException If the used token is invalid. |
|
292 | + */ |
|
293 | + private function checkErrors($response) |
|
294 | + { |
|
295 | + if (strpos($response, 'Rate limit exceeded.') !== false) { |
|
296 | + throw new RateLimitExceedException("You exceed the rate limit.", $response); |
|
297 | + } elseif (strpos($response, 'Unknown token.') !== false) { |
|
298 | + throw new InvalidTokenException("The used token is invalid.", $response); |
|
299 | + } |
|
300 | + } |
|
301 | 301 | } |