Skip to content

Commit bcd07e0

Browse files
authored
Merge pull request #115 from irtaza100/master
Add support for ipgeolocation.io
2 parents 064a548 + 9c0d5b8 commit bcd07e0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

config/geoip.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@
7272
'continent_path' => storage_path('app/continents.json'),
7373
'lang' => 'en',
7474
],
75+
76+
'ipgeolocation' => [
77+
'class' => \Torann\GeoIP\Services\IPGeoLocation::class,
78+
'secure' => true,
79+
'key' => env('IPGEOLOCATION_KEY'),
80+
'continent_path' => storage_path('app/continents.json'),
81+
'lang' => 'en',
82+
],
7583

7684
],
7785

src/Services/IPGeoLocation.php

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

0 commit comments

Comments
 (0)