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