From 13da68001fd8fca62e9140a157ab7b93087082c5 Mon Sep 17 00:00:00 2001 From: Marnus van Niekerk Date: Mon, 29 Apr 2024 12:31:01 +0200 Subject: [PATCH 1/3] Add IPInfo.io service --- config/geoip.php | 6 ++++ src/Services/IPInfo.php | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/Services/IPInfo.php diff --git a/config/geoip.php b/config/geoip.php index 0f6570e..fc9ea5a 100644 --- a/config/geoip.php +++ b/config/geoip.php @@ -94,6 +94,12 @@ 'locales' => ['en'], ], + 'ipinfo' => [ + 'class' => \Torann\GeoIP\Services\IPInfo::class, + 'key' => env('IPINFO_API_KEY'), + 'secure' => true, + ], + ], /* diff --git a/src/Services/IPInfo.php b/src/Services/IPInfo.php new file mode 100644 index 0000000..745fbad --- /dev/null +++ b/src/Services/IPInfo.php @@ -0,0 +1,65 @@ +client = new HttpClient([ + 'base_uri' => 'https://ipinfo.io/', + 'query' => [ + 'token' => $this->config('key'), + ], + ]); + } + + /** + * {@inheritdoc} + * @throws Exception + */ + public function locate($ip) + { + // Get data from client + $data = $this->client->get($ip); + + // Verify server response + if ($this->client->getErrors() !== null || empty($data[0])) { + throw new Exception('Request failed (' . $this->client->getErrors() . ')'); + } + + $json = json_decode($data[0], true); + + return $this->hydrate([ + 'ip' => $ip, + 'iso_code' => $json['country'], + 'country' => $json['country'], + 'city' => $json['city'], + 'state' => $json['region'], + 'state_name' => $json['region'], + 'postal_code' => $json['postal'], + 'timezone' => $json['timezone'], + 'continent' => $json['continent'] ?? explode('/',$json['timezone'])[0] ?? '', + ]); + } +} From aa005693069afa64da05cd50c8a2a3162628027b Mon Sep 17 00:00:00 2001 From: Marnus van Niekerk Date: Thu, 2 May 2024 08:22:47 +0200 Subject: [PATCH 2/3] Add country name lookup --- src/Services/IPInfo.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Services/IPInfo.php b/src/Services/IPInfo.php index 745fbad..e85a1b7 100644 --- a/src/Services/IPInfo.php +++ b/src/Services/IPInfo.php @@ -53,7 +53,7 @@ public function locate($ip) return $this->hydrate([ 'ip' => $ip, 'iso_code' => $json['country'], - 'country' => $json['country'], + 'country' => $this->get_country_name($json['country']), 'city' => $json['city'], 'state' => $json['region'], 'state_name' => $json['region'], @@ -62,4 +62,12 @@ public function locate($ip) 'continent' => $json['continent'] ?? explode('/',$json['timezone'])[0] ?? '', ]); } + + public function get_country_name($country_code) + { + $url = 'https://restcountries.com/v3.1/alpha/' . $country_code; + $country_data = @json_decode(@file_get_contents($url)); + + return $country_data[0]->name->common ?? $country_code; + } } From 70df9c01ab20e96127973222964a832846945ece Mon Sep 17 00:00:00 2001 From: Marnus van Niekerk Date: Thu, 2 May 2024 08:23:50 +0200 Subject: [PATCH 3/3] Null protection --- src/Services/IPInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/IPInfo.php b/src/Services/IPInfo.php index e85a1b7..76ce0dd 100644 --- a/src/Services/IPInfo.php +++ b/src/Services/IPInfo.php @@ -68,6 +68,6 @@ public function get_country_name($country_code) $url = 'https://restcountries.com/v3.1/alpha/' . $country_code; $country_data = @json_decode(@file_get_contents($url)); - return $country_data[0]->name->common ?? $country_code; + return $country_data[0]?->name?->common ?? $country_code; } }