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, + ] + ], /* diff --git a/src/Services/IPQuery.php b/src/Services/IPQuery.php new file mode 100644 index 0000000..ca6040f --- /dev/null +++ b/src/Services/IPQuery.php @@ -0,0 +1,59 @@ + "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)['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']); + } +}