Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit b7d5bd3

Browse files
author
Dan Cryer
committed
Add support for IPData.co API key.
1 parent d2d8594 commit b7d5bd3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/AddressLocator/IpLocator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ class IpLocator
1616
/** @var string */
1717
protected $ip;
1818

19+
/** @var string */
20+
protected $apiKey;
21+
1922
public function __construct(string $ip)
2023
{
24+
$this->apiKey = config('services.ipdata.key', null);
25+
26+
if (empty($this->apiKey)) {
27+
throw new BadRequestException('Cannot perform IP lookup without a valid IPData.co API key.');
28+
}
29+
2130
if (!self::isValidIp($ip)) {
2231
throw new BadRequestException($ip . ' is not a valid IP address.');
2332
}
@@ -54,7 +63,8 @@ public function locate() : Address
5463

5564
try {
5665
$client = new Client();
57-
$response = $client->get(self::SERVICE_URI . $this->ip);
66+
$url = self::SERVICE_URI . $this->ip . '?api-key=' . $this->apiKey;
67+
$response = $client->get($url);
5868
} catch (\Exception $ex) {
5969
throw new HttpException($ex->getMessage(), $ex->getCode(), $ex);
6070
}

0 commit comments

Comments
 (0)