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
|
|
|
* Http client instance. |
20
|
|
|
* |
21
|
|
|
* @var \GuzzleHttp\Client |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A collection of continents. |
27
|
|
|
* |
28
|
|
|
* @var \Arcanedev\GeoIP\Entities\Continents |
29
|
|
|
*/ |
30
|
|
|
protected $continents; |
31
|
|
|
|
32
|
|
|
/* ------------------------------------------------------------------------------------------------ |
33
|
|
|
| Main Functions |
34
|
|
|
| ------------------------------------------------------------------------------------------------ |
35
|
|
|
*/ |
36
|
|
|
/** |
37
|
|
|
* Init the driver. |
38
|
|
|
*/ |
39
|
60 |
|
protected function init() |
40
|
|
|
{ |
41
|
60 |
|
$this->client = new Client([ |
42
|
60 |
|
'base_uri' => 'http://freegeoip.net/', |
43
|
|
|
'headers' => [ |
44
|
10 |
|
'User-Agent' => 'Laravel-GeoIP', |
45
|
|
|
] |
46
|
10 |
|
]); |
47
|
|
|
|
48
|
60 |
|
$this->continents = Continents::make(); |
49
|
60 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Locate the ip address. |
53
|
|
|
* |
54
|
|
|
* @param string $ipAddress |
55
|
|
|
* |
56
|
|
|
* @return \Arcanedev\GeoIP\Location |
57
|
|
|
*/ |
58
|
18 |
|
public function locate($ipAddress) |
59
|
|
|
{ |
60
|
18 |
|
$response = $this->client->get("json/$ipAddress"); |
61
|
|
|
|
62
|
12 |
|
$data = json_decode($response->getBody()); |
63
|
|
|
|
64
|
12 |
|
return $this->hydrate([ |
65
|
13 |
|
'ip' => $ipAddress, |
66
|
12 |
|
'iso_code' => $data->country_code, |
67
|
12 |
|
'country' => $data->country_name, |
68
|
12 |
|
'city' => $data->city, |
69
|
12 |
|
'state' => $data->region_name, |
70
|
12 |
|
'state_code' => $data->region_code, |
71
|
12 |
|
'postal_code' => $data->zip_code, |
72
|
12 |
|
'latitude' => $data->latitude, |
73
|
12 |
|
'longitude' => $data->longitude, |
74
|
12 |
|
'timezone' => $data->time_zone, |
75
|
12 |
|
'continent' => $this->continents->get($data->country_code, 'Unknown'), |
76
|
2 |
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Update function for service. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
|
|
public function update() |
87
|
|
|
{ |
88
|
|
|
// Do nothing |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|