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