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