/
GeoLookup.php
144 lines (125 loc) 路 3.54 KB
/
GeoLookup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace IPStack\PHP;
use GuzzleHttp\Client;
use TixAstronauta\AccIp\AccIp;
/**
* A PHP class for querying an IPStack server
* for the Geo Location information of an IP.
*/
class GeoLookup
{
/**
* The timeout for the current server.
*
* @var int
*/
private $timeout;
/**
* The API key used to connect to the IPStack API.
*
* @var string
*/
private $api_key;
/**
* If set to true, HTTPS will be used for the connection.
*
* @var bool
*/
private $use_https;
/**
* Construct the FreeGeoIp object with server information.
* Defaults to freegeoip.net.
*
* @param string $api_key
* @param bool $use_https
* @param int $timeout
*/
public function __construct(
string $api_key,
bool $use_https = false,
int $timeout = 10
) {
$this->timeout = $timeout;
$this->api_key = $api_key;
$this->use_https = $use_https;
}
/**
* Retrieve a location for a specific IP address.
*
* @param string $ip
*
* @return \FreeGeoIp\PHP\Location|null
* @throws \Exception
*/
public function getLocation(string $ip)
{
$ret = null;
try {
$response = (new Client([
'base_uri' => (
($this->use_https)
? 'https'
: 'http'
).'://api.ipstack.com/',
'timeout' => $this->timeout,
]))->get($ip.'?access_key='.$this->api_key.'&output=json');
if ($response->getStatusCode() == 200) {
$compiled = json_decode($response->getBody()->getContents(), true);
if (array_key_exists('error', $compiled)) {
throw new \Exception('Error: '.$compiled['error']['info']);
}
$ret = new Location($compiled);
}
} catch (\Exception $e) {
throw $e;
}
return $ret;
}
/**
* Retrieve the location information for a batch of IP addresses.
*
* @param string ...$ips The IP addresses.
*
* @return \FreeGeoIp\PHP\Location|null
* @throws \Exception
*/
public function getLocations(string ...$ips)
{
$ret = null;
try {
$response = (new Client([
'base_uri' => (
($this->use_https)
? 'https'
: 'http'
).'://api.ipstack.com/',
'timeout' => $this->timeout,
]))->get(implode(',', $ips).'?access_key='.$this->api_key.'&output=json');
if ($response->getStatusCode() == 200) {
$compiled = json_decode($response->getBody()->getContents(), true);
if (array_key_exists('error', $compiled)) {
throw new \Exception('Error: '.$compiled['error']['info']);
}
$ret = new Location($compiled);
}
} catch (\Exception $e) {
throw $e;
}
return $ret;
}
/**
* Returns a location for the current clients IP address.
*
* @return \FreeGeoIp\PHP\Location
* @throws \Exception
*/
public function getClientLocation()
{
$accIp = new AccIp();
$ip = $accIp->getIpAddress();
if ($ip === false) {
throw new \Exception('Error: Unable to find client IP address.');
}
return $this->getLocationFor($ip);
}
}