diff --git a/ReadMe.md b/ReadMe.md index 15faa7e..9b9f5cb 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -49,7 +49,7 @@ try { // // This function will work with hostnames // or IP addresses. - $location = $geoLookup->getLocationFor('github.com'); + $location = $geoLookup->getLocation('github.com'); // You can alternately look up the information // for the current client's IP address. diff --git a/src/IPStack/GeoLookup.php b/src/IPStack/GeoLookup.php index ef37284..fd038ce 100644 --- a/src/IPStack/GeoLookup.php +++ b/src/IPStack/GeoLookup.php @@ -58,7 +58,7 @@ public function __construct( * @return \FreeGeoIp\PHP\Location|null * @throws \Exception */ - public function getLocationFor(string $ip) + public function getLocation(string $ip) { $ret = null; @@ -87,6 +87,43 @@ public function getLocationFor(string $ip) return $ret; } + /** + * Retrieve the location information for a batch of IP addresses. + * + * @param string ...$ips The IP addresses. + * + * @return \FreeGeoIp\PHP\Location|null + * @throws \Exception + */ + public function getLocations(string ...$ips) + { + $ret = null; + + try { + $response = (new Client([ + 'base_uri' => ( + ($this->use_https) + ? 'https' + : 'http' + ).'://api.ipstack.com/', + 'timeout' => $this->timeout, + ]))->get(implode(',', $ips).'?access_key='.$this->api_key.'&output=json'); + + if ($response->getStatusCode() == 200) { + $compiled = json_decode($response->getBody()->getContents(), true); + if (array_key_exists('error', $compiled)) { + throw new \Exception('Error: '.$compiled['error']['info']); + } + + $ret = new Location($compiled); + } + } catch (\Exception $e) { + throw $e; + } + + return $ret; + } + /** * Returns a location for the current clients IP address. * diff --git a/tests/GeoLookupTest.php b/tests/GeoLookupTest.php index c5bfd5a..96bcbc1 100644 --- a/tests/GeoLookupTest.php +++ b/tests/GeoLookupTest.php @@ -14,7 +14,7 @@ final class GeoLookupTest extends TestCase public function testGetLocationForReturnsLocationObject() { $geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90'); - $location = $geo->getLocationFor('github.com'); + $location = $geo->getLocation('github.com'); $this->assertInstanceOf(Location::class, $location); } @@ -26,7 +26,7 @@ public function testGetLocationWithHttpsForReturnsLocationObjectOnInvalidPlan() $this->expectException(\Exception::class); $this->expectExceptionMessage('Error: Access Restricted - Your current Subscription Plan does not support HTTPS Encryption.'); - $location = $geo->getLocationFor('github.com'); + $location = $geo->getLocation('github.com'); $this->assertInstanceOf(Location::class, $location); } @@ -38,8 +38,20 @@ public function testGetClientLocationOnUnableToFindClientIp() $this->expectException(\Exception::class); $this->expectExceptionMessage('Error: Unable to find client IP address.'); - $location = $geo->getClientLocation('github.com'); + $location = $geo->getClientLocation(); $this->assertInstanceOf(Location::class, $location); - } + } + + public function testGetLocationsBulkRequestReturnsErrorOnMissingAPIPermissions() + { + $geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90'); + + $this->expectException(\Exception::class); + $this->expectExceptionmessage('Error: Bulk requests are not supported on your plan. Please upgrade your subscription.'); + + $location = $geo->getLocations('1.1.1.1', '2.2.2.2'); + + $this->assertInstanceOf(Location::class, $location); + } }