From 42f0ab4705e89107aea79a8d7a4f80b97eba8fa6 Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Tue, 17 Sep 2024 14:17:09 +0600 Subject: [PATCH] ipapi response structured --- src/Services/Vendors/GeoIp/IpApi.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Services/Vendors/GeoIp/IpApi.php b/src/Services/Vendors/GeoIp/IpApi.php index 49be84e..a4d044d 100644 --- a/src/Services/Vendors/GeoIp/IpApi.php +++ b/src/Services/Vendors/GeoIp/IpApi.php @@ -26,14 +26,24 @@ public function __construct(array $config = []) public function find(string $ip): mixed { + $response = Http::baseUrl("https://api.ipapi.com/api/") + ->contentType('application/json') + ->acceptJson() + ->get($ip, [ + 'access_key' => $this->token, + 'output' => 'json', + 'language' => 'en' + ]); + + if (!$response->json()) { + throw new \JsonException("Invalid IP API Response."); + } - $response = Http::baseUrl("https://api.ipapi.com/api/")->get($ip, [ - 'access_key' => $this->token, - 'output' => 'json', - 'language' => 'en' - ]); + if (!$response->json('success')) { + throw new \JsonException("IP API Error: " . $response->json('error.info')); + } - return $response->json(); + return $response; } /**