1
|
|
|
<?php namespace Arcanedev\GeoIP\Drivers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoIP\Entities\Continents; |
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FreeGeoIpDriver |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\GeoIP\Drivers |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FreeGeoIpDriver extends AbstractDriver |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Http client instance. |
21
|
|
|
* |
22
|
|
|
* @var \GuzzleHttp\Client |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A collection of continents. |
28
|
|
|
* |
29
|
|
|
* @var \Arcanedev\GeoIP\Entities\Continents |
30
|
|
|
*/ |
31
|
|
|
protected $continents; |
32
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
34
|
|
|
| Main Methods |
35
|
|
|
| ----------------------------------------------------------------- |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Init the driver. |
40
|
|
|
*/ |
41
|
30 |
|
protected function init() |
42
|
|
|
{ |
43
|
30 |
|
$this->client = new Client([ |
44
|
30 |
|
'base_uri' => 'http://freegeoip.net/', |
45
|
|
|
'headers' => [ |
46
|
|
|
'User-Agent' => 'Laravel-GeoIP', |
47
|
|
|
] |
48
|
|
|
]); |
49
|
|
|
|
50
|
30 |
|
$this->continents = Continents::load(); |
51
|
30 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Locate the ip address. |
55
|
|
|
* |
56
|
|
|
* @param string $ipAddress |
57
|
|
|
* |
58
|
|
|
* @return \Arcanedev\GeoIP\Location |
59
|
|
|
*/ |
60
|
9 |
|
public function locate($ipAddress) |
61
|
|
|
{ |
62
|
9 |
|
$response = $this->client->get("json/$ipAddress"); |
63
|
|
|
|
64
|
6 |
|
$data = json_decode($response->getBody()); |
65
|
|
|
|
66
|
6 |
|
return $this->hydrate([ |
67
|
6 |
|
'ip' => $ipAddress, |
68
|
6 |
|
'iso_code' => $data->country_code, |
69
|
6 |
|
'country' => $data->country_name, |
70
|
6 |
|
'city' => $data->city, |
71
|
6 |
|
'state' => $data->region_name, |
72
|
6 |
|
'state_code' => $data->region_code, |
73
|
6 |
|
'postal_code' => $data->zip_code, |
74
|
6 |
|
'latitude' => $data->latitude, |
75
|
6 |
|
'longitude' => $data->longitude, |
76
|
6 |
|
'timezone' => $data->time_zone, |
77
|
6 |
|
'continent' => $this->continents->get($data->country_code, 'Unknown'), |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|