diff --git a/config/ip-geolocation.php b/config/ip-geolocation.php index 403eee3..e6054e2 100644 --- a/config/ip-geolocation.php +++ b/config/ip-geolocation.php @@ -6,7 +6,7 @@ | IPGeolocation Driver Type |-------------------------------------------------------------------------- | - | Supported: "ip-api", "maxmind_database", "maxmind_api", "ipstack", "ipquery" + | Supported: "ip-api", "maxmind_database", "maxmind_api", "ipstack", "ipquery", "ip2geo" | */ 'driver' => env('IPGEOLOCATION_DRIVER', 'ip-api'), @@ -95,4 +95,14 @@ // Get your API key here: https://ipquery.io 'key' => env('IPGEOLOCATION_IPQUERY_KEY'), ], + + /* + |-------------------------------------------------------------------------- + | ip2geo Driver + |-------------------------------------------------------------------------- + */ + 'ip2geo' => [ + // Get your API key here: https://ip2geo.dev + 'key' => env('IPGEOLOCATION_IP2GEO_KEY'), + ], ]; diff --git a/src/Drivers/IP2GeoDriver.php b/src/Drivers/IP2GeoDriver.php new file mode 100644 index 0000000..9e07560 --- /dev/null +++ b/src/Drivers/IP2GeoDriver.php @@ -0,0 +1,71 @@ +getRaw($ip); + + if (empty($data) || ! Arr::get($data, 'success')) { + return $this->getDefault(); + } + + $d = Arr::get($data, 'data', []); + $continent = Arr::get($d, 'continent', []); + $country = Arr::get($continent, 'country', []); + $city = Arr::get($country, 'city', []); + $subdivision = Arr::get($country, 'subdivision', []); + $timezone = Arr::get($city, 'timezone', []); + + return [ + 'city' => Arr::get($city, 'name'), + 'country' => Arr::get($country, 'name'), + 'countryCode' => Arr::get($country, 'code'), + 'latitude' => Arr::get($city, 'latitude') !== null + ? (float) number_format(Arr::get($city, 'latitude'), 5) + : null, + 'longitude' => Arr::get($city, 'longitude') !== null + ? (float) number_format(Arr::get($city, 'longitude'), 5) + : null, + 'region' => Arr::get($subdivision, 'name'), + 'regionCode' => Arr::get($subdivision, 'code'), + 'timezone' => Arr::get($timezone, 'name'), + 'postalCode' => Arr::get($city, 'postal_code'), + ]; + } + + /** + * Get the raw IPGeolocation info using ip2geo. + * + * @param string $ip + * @return array + */ + public function getRaw($ip) + { + $url = 'https://api.ip2geo.dev/convert?ip=' . urlencode($ip); + + try { + return json_decode( + $this->guzzle->get($url, [ + 'headers' => [ + 'X-Api-Key' => Arr::get($this->config, 'key'), + ], + ])->getBody(), + true + ); + } catch (\Throwable $e) { + return []; + } + } +} diff --git a/src/IPGeolocationManager.php b/src/IPGeolocationManager.php index 9462d10..6018a62 100644 --- a/src/IPGeolocationManager.php +++ b/src/IPGeolocationManager.php @@ -5,6 +5,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; use GuzzleHttp\Client as GuzzleClient; +use PulkitJalan\IPGeolocation\Drivers\IP2GeoDriver; use PulkitJalan\IPGeolocation\Drivers\IPApiDriver; use PulkitJalan\IPGeolocation\Drivers\IPInfoDriver; use PulkitJalan\IPGeolocation\Drivers\IPQueryDriver; @@ -104,4 +105,12 @@ protected function createIpqueryDriver(array $data): IPQueryDriver { return new IPQueryDriver($data, $this->guzzle); } + + /** + * Get the ip2geo driver. + */ + protected function createIp2geoDriver(array $data): IP2GeoDriver + { + return new IP2GeoDriver($data, $this->guzzle); + } }