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
|
|
|
/** |
19
|
|
|
* Http client instance. |
20
|
|
|
* |
21
|
|
|
* @var \GeoIp2\WebService\Client |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Main Methods |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Init the driver. |
32
|
|
|
*/ |
33
|
3 |
|
protected function init() |
34
|
|
|
{ |
35
|
3 |
|
$this->client = new Client( |
36
|
3 |
|
$this->getOption('user_id'), |
37
|
3 |
|
$this->getOption('license_key'), |
38
|
3 |
|
$this->getOption('locales', ['en']) |
39
|
|
|
); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Locate the ip address. |
44
|
|
|
* |
45
|
|
|
* @param string $ipAddress |
46
|
|
|
* |
47
|
|
|
* @return \Arcanedev\GeoIP\Location |
48
|
|
|
*/ |
49
|
|
|
public function locate($ipAddress) |
50
|
|
|
{ |
51
|
|
|
$record = $this->client->city($ipAddress); |
52
|
|
|
|
53
|
|
|
return $this->hydrate([ |
54
|
|
|
'ip' => $ipAddress, |
55
|
|
|
'iso_code' => $record->country->isoCode, |
56
|
|
|
'country' => $record->country->name, |
57
|
|
|
'city' => $record->city->name, |
58
|
|
|
'state' => $record->mostSpecificSubdivision->name, |
59
|
|
|
'state_code' => $record->mostSpecificSubdivision->isoCode, |
60
|
|
|
'postal_code' => $record->postal->code, |
61
|
|
|
'latitude' => $record->location->latitude, |
62
|
|
|
'longitude' => $record->location->longitude, |
63
|
|
|
'timezone' => $record->location->timeZone, |
64
|
|
|
'continent' => $record->continent->code, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|