@@ -6,11 +6,11 @@ |
||
6 | 6 | */ |
7 | 7 | class IpInfoException extends \Exception |
8 | 8 | { |
9 | - private $fullMessage; |
|
9 | + private $fullMessage; |
|
10 | 10 | |
11 | - public function __construct($error, $fullMessage) |
|
12 | - { |
|
13 | - parent::__construct($error); |
|
14 | - $this->fullMessage = $fullMessage; |
|
15 | - } |
|
11 | + public function __construct($error, $fullMessage) |
|
12 | + { |
|
13 | + parent::__construct($error); |
|
14 | + $this->fullMessage = $fullMessage; |
|
15 | + } |
|
16 | 16 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function getYourOwnIpDetails() |
122 | 122 | { |
123 | - $response = $this->makeCurlRequest($this::BASE_URL.'json'); |
|
123 | + $response = $this->makeCurlRequest($this::BASE_URL . 'json'); |
|
124 | 124 | $response = $this->jsonDecodeResponse($response); |
125 | 125 | |
126 | 126 | return new Host($response); |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function getFullIpDetails($ipAddress) |
137 | 137 | { |
138 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); |
|
138 | + $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress); |
|
139 | 139 | $response = $this->jsonDecodeResponse($response); |
140 | 140 | |
141 | 141 | return new Host($response); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | public function getSpecificField($ipAddress, $field) |
155 | 155 | { |
156 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); |
|
156 | + $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress . '/' . $field); |
|
157 | 157 | $response = $this->checkGeo($field, $response); |
158 | 158 | |
159 | 159 | return $response; |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | */ |
171 | 171 | public function getYourOwnIpSpecificField($field) |
172 | 172 | { |
173 | - $response = $this->makeCurlRequest($this::BASE_URL.$field); |
|
173 | + $response = $this->makeCurlRequest($this::BASE_URL . $field); |
|
174 | 174 | $response = $this->checkGeo($field, $response); |
175 | 175 | |
176 | 176 | return $response; |
@@ -224,11 +224,11 @@ discard block |
||
224 | 224 | $curl = curl_init(); |
225 | 225 | |
226 | 226 | if (!empty($this->settings['token'])) { |
227 | - $address .= '?token='.$this->settings['token']; |
|
227 | + $address .= '?token=' . $this->settings['token']; |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | if ($this->settings['debug']) { |
231 | - echo 'Request address: '.$address."\n"; |
|
231 | + echo 'Request address: ' . $address . "\n"; |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | curl_setopt_array($curl, array( |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | |
241 | 241 | if ($response === false && $this->settings['debug']) { |
242 | 242 | $error = curl_error($curl); |
243 | - echo "The error is".$error; |
|
243 | + echo "The error is" . $error; |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | curl_close($curl); |
@@ -12,274 +12,274 @@ |
||
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 | - 'debug' => false, |
|
113 | - ), $settings); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Get all the info about your own ip address. |
|
118 | - * |
|
119 | - * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
120 | - */ |
|
121 | - public function getYourOwnIpDetails() |
|
122 | - { |
|
123 | - $response = $this->makeCurlRequest($this::BASE_URL.'json'); |
|
124 | - $response = $this->jsonDecodeResponse($response); |
|
125 | - |
|
126 | - return new Host($response); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get all the info about an ip address. |
|
131 | - * |
|
132 | - * @param string $ipAddress The ip address. |
|
133 | - * |
|
134 | - * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
135 | - */ |
|
136 | - public function getFullIpDetails($ipAddress) |
|
137 | - { |
|
138 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); |
|
139 | - $response = $this->jsonDecodeResponse($response); |
|
140 | - |
|
141 | - return new Host($response); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Get a specific field value. |
|
146 | - * |
|
147 | - * @param string $ipAddress The ip address. |
|
148 | - * @param string $field The field. |
|
149 | - * |
|
150 | - * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
|
151 | - * This could returns an Host object if you call it with for the field |
|
152 | - * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
153 | - */ |
|
154 | - public function getSpecificField($ipAddress, $field) |
|
155 | - { |
|
156 | - $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); |
|
157 | - $response = $this->checkGeo($field, $response); |
|
158 | - |
|
159 | - return $response; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Get a specific field value of your own ip address. |
|
164 | - * |
|
165 | - * @param string $field The field. |
|
166 | - * |
|
167 | - * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
|
168 | - * This could returns an Host object if you call it with for the field |
|
169 | - * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
170 | - */ |
|
171 | - public function getYourOwnIpSpecificField($field) |
|
172 | - { |
|
173 | - $response = $this->makeCurlRequest($this::BASE_URL.$field); |
|
174 | - $response = $this->checkGeo($field, $response); |
|
175 | - |
|
176 | - return $response; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Use the /geo call to get just the geolocation information, which will often be |
|
181 | - * faster than getting the full response. |
|
182 | - * |
|
183 | - * @param string $ipAddress The ip address. |
|
184 | - * |
|
185 | - * @return \DavidePastore\Ipinfo\Host |
|
186 | - */ |
|
187 | - public function getIpGeoDetails($ipAddress) |
|
188 | - { |
|
189 | - return $this->getSpecificField($ipAddress, $this::GEO); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Check if the response is GEO and set the parameters accordingly. |
|
194 | - * |
|
195 | - * @param string $field The field value. |
|
196 | - * @param string $response The response from the server. |
|
197 | - * |
|
198 | - * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is |
|
199 | - * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
|
200 | - * delete the last character ('\n'). |
|
201 | - */ |
|
202 | - private function checkGeo($field, $response) |
|
203 | - { |
|
204 | - if ($field == $this::GEO) { |
|
205 | - $response = $this->jsonDecodeResponse($response); |
|
206 | - $response = new Host($response); |
|
207 | - } else { |
|
208 | - $this->checkErrors($response); |
|
209 | - $response = substr($response, 0, -1); |
|
210 | - } |
|
211 | - |
|
212 | - return $response; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Make a curl request. |
|
217 | - * |
|
218 | - * @param string $address The address of the request. |
|
219 | - * |
|
220 | - * @return string Returns the response from the request. |
|
221 | - */ |
|
222 | - private function makeCurlRequest($address) |
|
223 | - { |
|
224 | - $curl = curl_init(); |
|
225 | - |
|
226 | - if (!empty($this->settings['token'])) { |
|
227 | - $address .= '?token='.$this->settings['token']; |
|
228 | - } |
|
229 | - |
|
230 | - if ($this->settings['debug']) { |
|
231 | - echo 'Request address: '.$address."\n"; |
|
232 | - } |
|
233 | - |
|
234 | - curl_setopt_array($curl, array( |
|
235 | - CURLOPT_RETURNTRANSFER => 1, |
|
236 | - CURLOPT_URL => $address, |
|
237 | - CURLOPT_CAINFO => __DIR__ . "/cacert.pem" |
|
238 | - )); |
|
239 | - |
|
240 | - $response = curl_exec($curl); |
|
241 | - |
|
242 | - if ($response === false && $this->settings['debug']) { |
|
243 | - $error = curl_error($curl); |
|
244 | - echo "The error is".$error; |
|
245 | - } |
|
246 | - |
|
247 | - curl_close($curl); |
|
248 | - |
|
249 | - return $response; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Returns the json decoded associative array. |
|
254 | - * @param string $response Response from the http call. |
|
255 | - * @return array Returns the associative array with the response. |
|
256 | - * @throws RateLimitExceedException If you exceed the rate limit. |
|
257 | - * @throws InvalidTokenException If the used token is invalid. |
|
258 | - */ |
|
259 | - private function jsonDecodeResponse($response) |
|
260 | - { |
|
261 | - if ($response) { |
|
262 | - // Check if the response contains an error message |
|
263 | - $this->checkErrors($response); |
|
264 | - $response = json_decode($response, true); |
|
265 | - } else { |
|
266 | - $response = array(); |
|
267 | - } |
|
268 | - return $response; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Check if the given response has some kind of errors. |
|
273 | - * @param string $response The response to check. |
|
274 | - * @throws RateLimitExceedException If you exceed the rate limit. |
|
275 | - * @throws InvalidTokenException If the used token is invalid. |
|
276 | - */ |
|
277 | - private function checkErrors($response) |
|
278 | - { |
|
279 | - if (strpos($response, 'Rate limit exceeded.') !== false) { |
|
280 | - throw new RateLimitExceedException("You exceed the rate limit.", $response); |
|
281 | - } elseif (strpos($response, 'Unknown token.') !== false) { |
|
282 | - throw new InvalidTokenException("The used token is invalid.", $response); |
|
283 | - } |
|
284 | - } |
|
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 | + 'debug' => false, |
|
113 | + ), $settings); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Get all the info about your own ip address. |
|
118 | + * |
|
119 | + * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
120 | + */ |
|
121 | + public function getYourOwnIpDetails() |
|
122 | + { |
|
123 | + $response = $this->makeCurlRequest($this::BASE_URL.'json'); |
|
124 | + $response = $this->jsonDecodeResponse($response); |
|
125 | + |
|
126 | + return new Host($response); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get all the info about an ip address. |
|
131 | + * |
|
132 | + * @param string $ipAddress The ip address. |
|
133 | + * |
|
134 | + * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
135 | + */ |
|
136 | + public function getFullIpDetails($ipAddress) |
|
137 | + { |
|
138 | + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); |
|
139 | + $response = $this->jsonDecodeResponse($response); |
|
140 | + |
|
141 | + return new Host($response); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Get a specific field value. |
|
146 | + * |
|
147 | + * @param string $ipAddress The ip address. |
|
148 | + * @param string $field The field. |
|
149 | + * |
|
150 | + * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
|
151 | + * This could returns an Host object if you call it with for the field |
|
152 | + * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
153 | + */ |
|
154 | + public function getSpecificField($ipAddress, $field) |
|
155 | + { |
|
156 | + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); |
|
157 | + $response = $this->checkGeo($field, $response); |
|
158 | + |
|
159 | + return $response; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Get a specific field value of your own ip address. |
|
164 | + * |
|
165 | + * @param string $field The field. |
|
166 | + * |
|
167 | + * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
|
168 | + * This could returns an Host object if you call it with for the field |
|
169 | + * \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
170 | + */ |
|
171 | + public function getYourOwnIpSpecificField($field) |
|
172 | + { |
|
173 | + $response = $this->makeCurlRequest($this::BASE_URL.$field); |
|
174 | + $response = $this->checkGeo($field, $response); |
|
175 | + |
|
176 | + return $response; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Use the /geo call to get just the geolocation information, which will often be |
|
181 | + * faster than getting the full response. |
|
182 | + * |
|
183 | + * @param string $ipAddress The ip address. |
|
184 | + * |
|
185 | + * @return \DavidePastore\Ipinfo\Host |
|
186 | + */ |
|
187 | + public function getIpGeoDetails($ipAddress) |
|
188 | + { |
|
189 | + return $this->getSpecificField($ipAddress, $this::GEO); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Check if the response is GEO and set the parameters accordingly. |
|
194 | + * |
|
195 | + * @param string $field The field value. |
|
196 | + * @param string $response The response from the server. |
|
197 | + * |
|
198 | + * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is |
|
199 | + * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
|
200 | + * delete the last character ('\n'). |
|
201 | + */ |
|
202 | + private function checkGeo($field, $response) |
|
203 | + { |
|
204 | + if ($field == $this::GEO) { |
|
205 | + $response = $this->jsonDecodeResponse($response); |
|
206 | + $response = new Host($response); |
|
207 | + } else { |
|
208 | + $this->checkErrors($response); |
|
209 | + $response = substr($response, 0, -1); |
|
210 | + } |
|
211 | + |
|
212 | + return $response; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Make a curl request. |
|
217 | + * |
|
218 | + * @param string $address The address of the request. |
|
219 | + * |
|
220 | + * @return string Returns the response from the request. |
|
221 | + */ |
|
222 | + private function makeCurlRequest($address) |
|
223 | + { |
|
224 | + $curl = curl_init(); |
|
225 | + |
|
226 | + if (!empty($this->settings['token'])) { |
|
227 | + $address .= '?token='.$this->settings['token']; |
|
228 | + } |
|
229 | + |
|
230 | + if ($this->settings['debug']) { |
|
231 | + echo 'Request address: '.$address."\n"; |
|
232 | + } |
|
233 | + |
|
234 | + curl_setopt_array($curl, array( |
|
235 | + CURLOPT_RETURNTRANSFER => 1, |
|
236 | + CURLOPT_URL => $address, |
|
237 | + CURLOPT_CAINFO => __DIR__ . "/cacert.pem" |
|
238 | + )); |
|
239 | + |
|
240 | + $response = curl_exec($curl); |
|
241 | + |
|
242 | + if ($response === false && $this->settings['debug']) { |
|
243 | + $error = curl_error($curl); |
|
244 | + echo "The error is".$error; |
|
245 | + } |
|
246 | + |
|
247 | + curl_close($curl); |
|
248 | + |
|
249 | + return $response; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Returns the json decoded associative array. |
|
254 | + * @param string $response Response from the http call. |
|
255 | + * @return array Returns the associative array with the response. |
|
256 | + * @throws RateLimitExceedException If you exceed the rate limit. |
|
257 | + * @throws InvalidTokenException If the used token is invalid. |
|
258 | + */ |
|
259 | + private function jsonDecodeResponse($response) |
|
260 | + { |
|
261 | + if ($response) { |
|
262 | + // Check if the response contains an error message |
|
263 | + $this->checkErrors($response); |
|
264 | + $response = json_decode($response, true); |
|
265 | + } else { |
|
266 | + $response = array(); |
|
267 | + } |
|
268 | + return $response; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Check if the given response has some kind of errors. |
|
273 | + * @param string $response The response to check. |
|
274 | + * @throws RateLimitExceedException If you exceed the rate limit. |
|
275 | + * @throws InvalidTokenException If the used token is invalid. |
|
276 | + */ |
|
277 | + private function checkErrors($response) |
|
278 | + { |
|
279 | + if (strpos($response, 'Rate limit exceeded.') !== false) { |
|
280 | + throw new RateLimitExceedException("You exceed the rate limit.", $response); |
|
281 | + } elseif (strpos($response, 'Unknown token.') !== false) { |
|
282 | + throw new InvalidTokenException("The used token is invalid.", $response); |
|
283 | + } |
|
284 | + } |
|
285 | 285 | } |