From 03f5d5b1b00dd531be686c5c8db8abd7f1bed844 Mon Sep 17 00:00:00 2001 From: hdaklue Date: Wed, 25 Dec 2024 23:34:21 +0200 Subject: [PATCH 1/3] IPQuery service added --- src/Services/IPQuery.php | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Services/IPQuery.php diff --git a/src/Services/IPQuery.php b/src/Services/IPQuery.php new file mode 100644 index 0000000..efa704b --- /dev/null +++ b/src/Services/IPQuery.php @@ -0,0 +1,53 @@ + "https://api.ipquery.io/", + ]; + + + $this->client = new HttpClient($base); + } + + /** + * {@inheritdoc} + * + * @throws Exception + */ + public function locate($ip) + { + // Get data from client + $data = $this->client->get("{$ip}",['format'=>'json']); + + // 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($json['location']); + } +} From 39c373d318429f28ef1adbd48df90e5d466e8d76 Mon Sep 17 00:00:00 2001 From: hdaklue Date: Wed, 25 Dec 2024 23:37:58 +0200 Subject: [PATCH 2/3] Update config --- config/geoip.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/geoip.php b/config/geoip.php index 8ebb9d7..e439eed 100644 --- a/config/geoip.php +++ b/config/geoip.php @@ -84,6 +84,11 @@ 'locales' => ['en'], ], + 'ipquery'=> [ + 'class' => \Torann\GeoIP\Services\IPQuery::class, + 'secure' => true, + ] + ], /* From 4cfa49435253c25f2a561beb4e541d153e7a2c96 Mon Sep 17 00:00:00 2001 From: hdaklue Date: Thu, 26 Dec 2024 00:36:36 +0200 Subject: [PATCH 3/3] Fallback missing currencies from API --- src/Services/IPQuery.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Services/IPQuery.php b/src/Services/IPQuery.php index efa704b..ca6040f 100644 --- a/src/Services/IPQuery.php +++ b/src/Services/IPQuery.php @@ -46,7 +46,13 @@ public function locate($ip) throw new Exception('Request failed (' . $this->client->getErrors() . ')'); } - $json = json_decode($data[0], true); + $json = json_decode($data[0], true)['location']; + // Check if currency is available IPQuery API doesn't provide it + + if (empty($json['currency'])) { + $currencies = require __DIR__.'/../support/currencies.php'; + $json['currency'] = $currencies[$json['country_code']]; + } return $this->hydrate($json['location']); }