Skip to content

Commit d00e9b5

Browse files
authored
Merge pull request #151 from ipfinder-io/ipf
add new Services ipfinder.io
2 parents 807fd68 + e9f2b6e commit d00e9b5

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

config/geoip.php

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
'secure' => true,
8888
],
8989

90+
'ipfinder' => [
91+
'class' => \Torann\GeoIP\Services\IPFinder::class,
92+
'key' => env('IPFINDER_API_KEY'),
93+
'secure' => true,
94+
'locales' => ['en'],
95+
],
96+
9097
],
9198

9299
/*

src/Services/IPFinder.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
/**
10+
* Class GeoIP
11+
* @package Torann\GeoIP\Services
12+
*/
13+
class IPFinder extends AbstractService
14+
{
15+
/**
16+
* Http client instance.
17+
*
18+
* @var HttpClient
19+
*/
20+
protected $client;
21+
22+
/**
23+
* The "booting" method of the service.
24+
*
25+
* @return void
26+
*/
27+
public function boot()
28+
{
29+
$this->client = new HttpClient([
30+
'base_uri' => 'https://api.ipfinder.io/v1/',
31+
'headers' => [
32+
'User-Agent' => 'Laravel-GeoIP-Torann',
33+
],
34+
'query' => [
35+
'token' => $this->config('key'),
36+
],
37+
]);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
* @throws Exception
43+
*/
44+
public function locate($ip)
45+
{
46+
// Get data from client
47+
$data = $this->client->get($ip);
48+
49+
// Verify server response
50+
if ($this->client->getErrors() !== null || empty($data[0])) {
51+
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
52+
}
53+
54+
$json = json_decode($data[0], true);
55+
56+
return $this->hydrate($json);
57+
}
58+
}

0 commit comments

Comments
 (0)