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