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
5 changes: 5 additions & 0 deletions config/geoip.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
'locales' => ['en'],
],

'ip2location' => [
'class' => \Torann\GeoIP\Services\IP2Location::class,
'key' => env('IP2LOCATION_API_KEY'),
],

],

/*
Expand Down
76 changes: 76 additions & 0 deletions src/Services/IP2Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Torann\GeoIP\Services;

use Exception;
use Torann\GeoIP\Location;
use Torann\GeoIP\Support\HttpClient;

class IP2Location extends AbstractService {

/**
* Http client instance.
*
* @var HttpClient
*/
protected HttpClient $client;

/**
* The "booting" method of the service.
*
* @return void
*/
public function boot(): void
{
$this->client = new HttpClient([
'base_uri' => 'http://api.ip2location.io/',
'query' => [
'key' => $this->config('key'),
],
]);
}

/**
* {@inheritdoc}
* @throws Exception
*/
public function locate($ip): array|Location
{
// Get data from client
$data = $this->client->get('find', [
'ip' => $ip,
]);

// Verify server response
if ($this->client->getErrors() !== null) {
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
}

// Parse body content
$json = json_decode($data[0]);

return $this->hydrate([
'ip' => $ip,
'iso_code' => $json->country_code,
'country' => $json->country_name,
'city' => $json->city_name,
'state' => null,
'state_name' => $json->region_name,
'postal_code' => $json->zip_code,
'lat' => $json->latitude,
'lon' => $json->longitude,
'timezone' => $json->time_zone,
'continent' => null,
]);
}

/**
* Update function for service.
*
* @return string
*/
public function update()
{
// Optional artisan command line update method
}
}