From ea1f6dc24a6c24c4d7eb5fe842207e8e523d2de0 Mon Sep 17 00:00:00 2001 From: Muhammad Fahli Saputra Date: Sun, 12 Jan 2025 20:19:39 +0700 Subject: [PATCH 1/2] feat: add ip2location service --- src/Services/IP2Location.php | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Services/IP2Location.php diff --git a/src/Services/IP2Location.php b/src/Services/IP2Location.php new file mode 100644 index 0000000..bc1ffd8 --- /dev/null +++ b/src/Services/IP2Location.php @@ -0,0 +1,76 @@ +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 + } +} From d7b6b55760813a4c532e3bd9c5d0bce8bd2d0afd Mon Sep 17 00:00:00 2001 From: Muhammad Fahli Saputra Date: Sun, 12 Jan 2025 20:19:56 +0700 Subject: [PATCH 2/2] feat: add ip2location configuration --- config/geoip.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/geoip.php b/config/geoip.php index 8ebb9d7..83f6c01 100644 --- a/config/geoip.php +++ b/config/geoip.php @@ -84,6 +84,11 @@ 'locales' => ['en'], ], + 'ip2location' => [ + 'class' => \Torann\GeoIP\Services\IP2Location::class, + 'key' => env('IP2LOCATION_API_KEY'), + ], + ], /*