|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavidePastore\Ipinfo; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* ipinfo.io service wrapper. |
|
7
|
|
|
* @author davidepastore |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Ipinfo |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The base url of the ipinfo service. |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
const BASE_URL = "http://ipinfo.io/"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The ip string. |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
const IP = "ip"; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The hostname string. |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
const HOSTNAME = "hostname"; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The loc string. |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const LOC = "loc"; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The org string. |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
const ORG = "org"; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The city string. |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
const CITY = "city"; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The region string. |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
const REGION = "region"; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The country string. |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
const COUNTRY = "country"; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The phone string. |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
const PHONE = "phone"; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The geo string. |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
const GEO = "geo"; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The postal string. |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
const POSTAL = "postal"; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* All the settings |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $settings; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create an Ipinfo instance. |
|
87
|
|
|
* @param array $settings An array with all the settings. |
|
88
|
|
|
* Supported keys are: |
|
89
|
|
|
* - token: string the developer token; |
|
90
|
|
|
* - debug: boolean active or not the debug. |
|
91
|
|
|
*/ |
|
92
|
8 |
|
public function __construct($settings = array()) |
|
93
|
|
|
{ |
|
94
|
|
|
//Merge user settings |
|
95
|
8 |
|
$this->settings = array_merge(array( |
|
96
|
8 |
|
'token' => '', |
|
97
|
|
|
'debug' => false |
|
98
|
8 |
|
), $settings); |
|
99
|
8 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get all the info about your own ip address. |
|
104
|
|
|
* @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getYourOwnIpDetails() |
|
107
|
|
|
{ |
|
108
|
1 |
|
$response = $this->makeCurlRequest($this::BASE_URL . "json"); |
|
109
|
1 |
|
$response = json_decode($response, true); |
|
110
|
1 |
|
return new Host($response); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get all the info about an ip address. |
|
115
|
|
|
* @param string $ipAddress The ip address. |
|
116
|
|
|
* @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function getFullIpDetails($ipAddress) |
|
119
|
|
|
{ |
|
120
|
2 |
|
$response = $this->makeCurlRequest($this::BASE_URL . $ipAddress); |
|
121
|
2 |
|
$response = json_decode($response, true); |
|
122
|
2 |
|
return new Host($response); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get a specific field value. |
|
127
|
|
|
* @param string $ipAddress The ip address. |
|
128
|
|
|
* @param string $field The field. |
|
129
|
|
|
* @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
|
130
|
|
|
* This could returns an Host object if you call it with for the field |
|
131
|
|
|
* \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function getSpecificField($ipAddress, $field) |
|
134
|
|
|
{ |
|
135
|
4 |
|
$response = $this->makeCurlRequest($this::BASE_URL . $ipAddress . "/" . $field); |
|
136
|
4 |
|
$response = $this->checkGeo($field, $response); |
|
137
|
4 |
|
return $response; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get a specific field value of your own ip address. |
|
142
|
|
|
* @param string $field The field. |
|
143
|
|
|
* @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
|
144
|
|
|
* This could returns an Host object if you call it with for the field |
|
145
|
|
|
* \DavidePastore\Ipinfo\Ipinfo::GEO. |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function getYourOwnIpSpecificField($field) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$response = $this->makeCurlRequest($this::BASE_URL . $field); |
|
150
|
1 |
|
$response = $this->checkGeo($field, $response); |
|
151
|
1 |
|
return $response; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Use the /geo call to get just the geolocation information, which will often be |
|
156
|
|
|
* faster than getting the full response. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $ipAddress The ip address. |
|
159
|
|
|
* @return \DavidePastore\Ipinfo\Host |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function getIpGeoDetails($ipAddress) |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->getSpecificField($ipAddress, $this::GEO); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Check if the response is GEO and set the parameters accordingly. |
|
168
|
|
|
* @param string $field The field value. |
|
169
|
|
|
* @param string $response The response from the server. |
|
170
|
|
|
* @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is |
|
171
|
|
|
* of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
|
172
|
|
|
* delete the last character ('\n'). |
|
173
|
|
|
*/ |
|
174
|
5 |
|
private function checkGeo($field, $response) |
|
175
|
|
|
{ |
|
176
|
5 |
|
if ($field == $this::GEO) { |
|
177
|
1 |
|
$response = json_decode($response, true); |
|
178
|
1 |
|
$response = new Host($response); |
|
179
|
1 |
|
} else { |
|
180
|
4 |
|
$response = substr($response, 0, -1); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
5 |
|
return $response; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Make a curl request. |
|
188
|
|
|
* @param string $address The address of the request. |
|
189
|
|
|
* @return string Returns the response from the request. |
|
190
|
|
|
*/ |
|
191
|
8 |
|
private function makeCurlRequest($address) |
|
192
|
|
|
{ |
|
193
|
8 |
|
$curl = curl_init(); |
|
194
|
|
|
|
|
195
|
8 |
|
if (!empty($this->settings['token'])) { |
|
196
|
1 |
|
$address .= "?token=" . $this->settings['token']; |
|
197
|
1 |
|
} |
|
198
|
|
|
|
|
199
|
8 |
|
if ($this->settings['debug']) { |
|
200
|
1 |
|
echo "Request address: " . $address . "\n"; |
|
201
|
1 |
|
} |
|
202
|
|
|
|
|
203
|
8 |
|
curl_setopt_array($curl, array( |
|
204
|
8 |
|
CURLOPT_RETURNTRANSFER => 1, |
|
205
|
8 |
|
CURLOPT_URL => $address |
|
206
|
8 |
|
)); |
|
207
|
|
|
|
|
208
|
8 |
|
$response = curl_exec($curl); |
|
209
|
|
|
|
|
210
|
8 |
|
curl_close($curl); |
|
211
|
|
|
|
|
212
|
8 |
|
return $response; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|