Skip to content

Commit 050dad0

Browse files
author
Mohamed Ben
committed
add new Services ipfinder.io
1 parent 807fd68 commit 050dad0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

config/geoip.php

Lines changed: 7 additions & 0 deletions
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

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

0 commit comments

Comments
 (0)