Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion config/ip-geolocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
],
];
71 changes: 71 additions & 0 deletions src/Drivers/IP2GeoDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace PulkitJalan\IPGeolocation\Drivers;

use Illuminate\Support\Arr;
use GuzzleHttp\Client as GuzzleClient;

class IP2GeoDriver extends AbstractIPGeolocationDriver implements IPGeolocationInterface
{
/**
* Get array of data using ip2geo.
*
* @param string $ip
* @return array
*/
public function get($ip)
{
$data = $this->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 [];
}
}
}
9 changes: 9 additions & 0 deletions src/IPGeolocationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}