Skip to content

Commit b2ed170

Browse files
authored
Merge pull request #117 from janicerar/master
Added support for ipdata.co
2 parents f6d70d6 + 75eaed4 commit b2ed170

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

config/geoip.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
'lang' => 'en',
8282
],
8383

84+
'ipdata' => [
85+
'class' => \Torann\GeoIP\Services\IPData::class,
86+
'key' => env('IPDATA_API_KEY'),
87+
'secure' => true,
88+
],
89+
8490
],
8591

8692
/*

src/Services/IPData.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Torann\GeoIP\Services;
4+
5+
use Exception;
6+
use Torann\GeoIP\Support\HttpClient;
7+
8+
/**
9+
* Class GeoIP
10+
* @package Torann\GeoIP\Services
11+
*/
12+
class IPData extends AbstractService
13+
{
14+
/**
15+
* Http client instance.
16+
*
17+
* @var HttpClient
18+
*/
19+
protected $client;
20+
21+
/**
22+
* The "booting" method of the service.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
$this->client = new HttpClient([
29+
'base_uri' => 'https://api.ipdata.co/',
30+
'query' => [
31+
'api-key' => $this->config('key'),
32+
],
33+
]);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
* @throws Exception
39+
*/
40+
public function locate($ip)
41+
{
42+
// Get data from client
43+
$data = $this->client->get($ip);
44+
45+
// Verify server response
46+
if ($this->client->getErrors() !== null || empty($data[0])) {
47+
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
48+
}
49+
50+
return $this->hydrate(json_decode($data[0], true));
51+
}
52+
}

0 commit comments

Comments
 (0)