Skip to content

Commit

Permalink
Merge 0bcdd38 into ca6228a
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaikelemen committed Nov 26, 2019
2 parents ca6228a + 0bcdd38 commit d48160c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
72 changes: 48 additions & 24 deletions src/IPStack/GeoLookup.php
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/IPStack/Legacy/FreeGeoIp.php
Expand Up @@ -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.'/';
Expand Down Expand Up @@ -87,7 +87,7 @@ public function getClientLocation()
throw new \Exception('Unable to find client IP address.');
}

return $this->getLocationFor($ip);
return $this->getLocation($ip);
}

/**
Expand Down

0 comments on commit d48160c

Please sign in to comment.