diff --git a/config/geoip.php b/config/geoip.php index 0f6570e..787992b 100644 --- a/config/geoip.php +++ b/config/geoip.php @@ -34,7 +34,8 @@ | Here you may specify the default storage driver that should be used | by the framework. | - | Supported: "maxmind_database", "maxmind_api", "ipapi" + | Supported: "maxmind_database", "maxmind_api", "ipapi", "ipgeolocation", + | "ipdata", "ipfinder", "apibundle" | */ @@ -94,6 +95,13 @@ 'locales' => ['en'], ], + 'apibundle' => [ + 'class' => \Torann\GeoIP\Services\ApiBundle::class, + 'key' => env('APIBUNDLE_API_KEY'), + 'secure' => true, + 'lang' => 'en', + ], + ], /* diff --git a/src/Services/ApiBundle.php b/src/Services/ApiBundle.php new file mode 100644 index 0000000..f95aa60 --- /dev/null +++ b/src/Services/ApiBundle.php @@ -0,0 +1,57 @@ + 'https://api.apibundle.io/ip-lookup?apikey=' . $this->config('key'), + ]; + + if ($this->config('lang')) { + $base['base_uri'] .= '&language=' . $this->config('lang'); + } + + $this->client = new HttpClient($base); + } + + /** + * {@inheritdoc} + * @throws Exception + */ + public function locate($ip) + { + // Get data from client + $data = $this->client->get('&ip=' . $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($json); + } +}