-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Niek Pijp
committed
Apr 12, 2022
1 parent
3796026
commit 3bb59f4
Showing
6 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
enum GeoLocator | ||
{ | ||
case IpRegistry; | ||
case IpStack; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Brainstud\UnlimitedGeolocation\GeoLocators; | ||
|
||
use Brainstud\UnlimitedGeolocation\Geolocation; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\RequestException; | ||
|
||
/** | ||
* Implementation of the IpStack geo-locator. | ||
*/ | ||
class IpStack implements GeoLocatorContract | ||
{ | ||
private Client $client; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = new Client(); | ||
} | ||
|
||
public function getGeolocation(string $ip): ?Geolocation | ||
{ | ||
if (! ($baseUrl = config('unlimited-geolocation.ip-stack.base_url')) | ||
|| ! ($apiKey = config('unlimited-geolocation.ip-stack.key')) | ||
) { | ||
return null; | ||
} | ||
|
||
try { | ||
$res = $this->client->request('GET', "$baseUrl/$ip?access_key=$apiKey"); | ||
$data = json_decode($res->getBody()->getContents()); | ||
|
||
return Geolocation::fromIpStack($data); | ||
} catch (RequestException $e) { | ||
if ($e->hasResponse() && $e->getResponse()->getStatusCode() === 400) { | ||
return null; | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters