|
1
|
|
|
<?php namespace Arcanedev\GeoIP\Drivers; |
|
2
|
|
|
|
|
3
|
|
|
use GeoIp2\WebService\Client; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class MaxmindApiDriver |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\GeoIP\Drivers |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class MaxmindApiDriver extends AbstractDriver |
|
12
|
|
|
{ |
|
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
14
|
|
|
| Properties |
|
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* Http client instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \GeoIp2\WebService\Client |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $client; |
|
23
|
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
25
|
|
|
| Main Functions |
|
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
27
|
|
|
*/ |
|
28
|
|
|
/** |
|
29
|
|
|
* Init the driver. |
|
30
|
|
|
*/ |
|
31
|
6 |
|
protected function init() |
|
32
|
|
|
{ |
|
33
|
6 |
|
$this->client = new Client( |
|
34
|
6 |
|
$this->getOption('user_id'), |
|
35
|
6 |
|
$this->getOption('license_key'), |
|
36
|
6 |
|
$this->getOption('locales', ['en']) |
|
37
|
3 |
|
); |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Locate the ip address. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $ipAddress |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Arcanedev\GeoIP\Location |
|
46
|
|
|
*/ |
|
47
|
|
|
public function locate($ipAddress) |
|
48
|
|
|
{ |
|
49
|
|
|
$record = $this->client->city($ipAddress); |
|
50
|
|
|
|
|
51
|
|
|
return $this->hydrate([ |
|
52
|
|
|
'ip' => $ipAddress, |
|
53
|
|
|
'iso_code' => $record->country->isoCode, |
|
54
|
|
|
'country' => $record->country->name, |
|
55
|
|
|
'city' => $record->city->name, |
|
56
|
|
|
'state' => $record->mostSpecificSubdivision->name, |
|
57
|
|
|
'state_code' => $record->mostSpecificSubdivision->isoCode, |
|
58
|
|
|
'postal_code' => $record->postal->code, |
|
59
|
|
|
'latitude' => $record->location->latitude, |
|
60
|
|
|
'longitude' => $record->location->longitude, |
|
61
|
|
|
'timezone' => $record->location->timeZone, |
|
62
|
|
|
'continent' => $record->continent->code, |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|