diff --git a/src/IPStack/GeoLookup.php b/src/IPStack/GeoLookup.php index 0912559..73e1b7b 100644 --- a/src/IPStack/GeoLookup.php +++ b/src/IPStack/GeoLookup.php @@ -65,30 +65,50 @@ public function __construct(string $api_key) $this->api_key = $api_key; } + /** + * Allows IPv6 addresses to be used. + * + * @param string $ip The IP to be formatted. + * + * @return string + */ + public function formatIp(string $ip) + { + if (\filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + return \urlencode($ip); + } + + return $ip; + } + /** * Retrieve a location for a specific IP address. * - * @param string $ip The IP to lookup. + * @param string $ip The IP to lookup. * * @return array|null * @throws \Exception */ public function getLocation(string $ip) { + $ip = $this->formatIp($ip); + try { - $response = (new Client([ + $response = (new Client( + [ 'base_uri' => ( ($this->use_https) ? 'https' : 'http' ).'://api.ipstack.com/', 'timeout' => $this->timeout, - ]))->get( + ] + ))->get( $ip.'?access_key='.$this->api_key. - '&output=json'. - ($this->find_hostname ? '&hostname=1' : ''). - ($this->assess_security ? '&security=1' : ''). - '&language='.$this->language + '&output=json'. + ($this->find_hostname ? '&hostname=1' : ''). + ($this->assess_security ? '&security=1' : ''). + '&language='.$this->language ); return $this->processResponse($response); @@ -112,19 +132,21 @@ public function getLocations(string ...$ips) } try { - $response = (new Client([ + $response = (new Client( + [ 'base_uri' => ( ($this->use_https) ? 'https' : 'http' ).'://api.ipstack.com/', 'timeout' => $this->timeout, - ]))->get( + ] + ))->get( implode(',', $ips).'?access_key='.$this->api_key. - '&output=json'. - ($this->find_hostname ? '&hostname=1' : ''). - ($this->assess_security ? '&security=1' : ''). - '&language='.$this->language + '&output=json'. + ($this->find_hostname ? '&hostname=1' : ''). + ($this->assess_security ? '&security=1' : ''). + '&language='.$this->language ); return $this->processResponse($response); @@ -142,19 +164,21 @@ public function getLocations(string ...$ips) public function getOwnLocation() { try { - $response = (new Client([ + $response = (new Client( + [ 'base_uri' => ( ($this->use_https) ? 'https' : 'http' ).'://api.ipstack.com/', 'timeout' => $this->timeout, - ]))->get( + ] + ))->get( 'check?access_key='.$this->api_key. - '&output=json'. - ($this->find_hostname ? '&hostname=1' : ''). - ($this->assess_security ? '&security=1' : ''). - '&language='.$this->language + '&output=json'. + ($this->find_hostname ? '&hostname=1' : ''). + ($this->assess_security ? '&security=1' : ''). + '&language='.$this->language ); return $this->processResponse($response); @@ -185,7 +209,7 @@ private function processResponse($response) /** * Returns a location for the current clients IP address. * - * @return \FreeGeoIp\PHP\Location + * @return array|null * @throws \Exception */ public function getClientLocation() @@ -197,7 +221,7 @@ public function getClientLocation() throw new \Exception('Error: Unable to find client IP address.'); } - return $this->getLocationFor($ip); + return $this->getLocation($ip); } /** @@ -206,7 +230,7 @@ public function getClientLocation() * * @param bool $value The new value. * - * @see https://ipstack.com/documentation#hostname + * @see https://ipstack.com/documentation#hostname * @return \IPStack\PHP\GeoLookup */ public function setFindHostname(bool $value) @@ -256,7 +280,7 @@ public function isUsingHttps() * * @param bool $value The new value. * - * @see https://ipstack.com/documentation#security + * @see https://ipstack.com/documentation#security * @return \IPStack\PHP\GeoLookup */ public function assessSecurity(bool $value) @@ -305,7 +329,7 @@ public function getTimeout() * * @param int $value The new language. * - * @see https://ipstack.com/documentation#language + * @see https://ipstack.com/documentation#language * @return \IPStack\PHP\GeoLookup */ public function setLanguage(string $language) diff --git a/src/IPStack/Legacy/FreeGeoIp.php b/src/IPStack/Legacy/FreeGeoIp.php index fe448c8..0779495 100644 --- a/src/IPStack/Legacy/FreeGeoIp.php +++ b/src/IPStack/Legacy/FreeGeoIp.php @@ -37,9 +37,9 @@ class FreeGeoIp */ public function __construct( string $server_address, - int $server_port = 8080, + int $server_port = 8080, string $server_protocol = 'http', - int $timeout = 10 + int $timeout = 10 ) { $this->server_url = $server_protocol.'://' .$server_address.':'.$server_port.'/'; @@ -87,7 +87,7 @@ public function getClientLocation() throw new \Exception('Unable to find client IP address.'); } - return $this->getLocationFor($ip); + return $this->getLocation($ip); } /**