This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gk_locateable.php
371 lines (341 loc) · 13.9 KB
/
gk_locateable.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
// Using xml2array to make life a little easier. If anyone would like to change this please send a pull
// request my way.
include_once('xml2array.php');
/*
* GkLocatable
* ===========
* This is a class that tries to wrap location based API calls into an easy to use single point of entry. It's still
* quite rough around the edges and would benefit greatly from some refactoring. At this point, the geocode_ip method
* does not seem to work.
*
*/
class GkLocateable {
// Used to store the settings
var $settings = array();
// Sets the settings
function setup( $settings = array()) {
$default = array(
'timeout' => 1000,
'yahoo_api_key' => '',
'google_api_key' => '',
'order' => array(
'yahoo','google','geocoder_ca', // 'geonames' need to implement
)
);
// Allow settings to be overridden
foreach($settings as $key=>$value) {
$default[$key] = $value;
}
// Set the settings
$this->settings = $default;
}
// A wrapper function that simply fires off a get request
function geocode($location) {
if(empty($location)) {
return false;
}
$response = $this->get_request($location);
return $response;
}
// Sends a CURL request to one of the location based services. If the response is invalid it will
// try one of the other services in the order defined within the settings.
function get_request($request) {
foreach($this->settings['order'] as $service) {
$request = call_user_func(array($this,$service."_request"),urlencode($request));
$ch = curl_init();
$options = array(
CURLOPT_URL => $request,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $this->settings['timeout']
);
curl_setopt_array($ch,$options);
$response = curl_exec($ch);
curl_close($ch);
if($response) {
$data = $this->parsedResponse($response,$service);
if($data['status']=='fail' || empty($data)) {
continue;
}else{
return $data;
}
}else{
continue;
}
}
return false;
}
// Parses our requests into a standard response
function parsedResponse($response,$service) {
$data = array(
'lat' => null,
'lng' => null,
'country' => '',
'city' => '',
'region' => '',
'type' => $service,
'status' => 'fail'
);
if($service=='google') {
$data = $this->parse_google_response($response);
}
elseif($service=='yahoo') {
$data = $this->parse_yahoo_response($response);
}
elseif($service=='geonames') {
$data = $this->parse_geonames_response($response);
}
elseif($service=='geocoder_us') {
$data = $this->parse_geocoder_us_response($response);
}
elseif($service=='geocoder_ca') {
$data = $this->parse_geocoder_ca_response($response);
}
return $data;
}
// Gets a rough lat and lng from an ip address
// Needs a rewrite very badly
function geocode_ip($ip) {
$ignore_ips = array(
'0.0.0.0','10.0.0.0','14.0.0.0',
'127.0.0.0','169.254.0.0','172.16.0.0',
'192.0.2.0','192.168.0.0','198.18.0.0',
'224.0.0.0','240.0.0.0'
);
if(in_array($ip,$ignore_ips)) {
return array('status'=>'fail');
}
$url = "http://api.hostip.info/get_html.php?ip=";
$url .= $ip;
$url .= "&position=true";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $this->settings['timeout']
);
curl_setopt_array($ch,$options);
$response = curl_exec($ch);
curl_close($ch);
if($response) {
$array_data = explode("\n",$response);
foreach($array_data as $line) {
if(!empty($line)) {
$data = explode(":",$line);
$data[1] = trim($data[1]);
if(!empty($data[0]) && !empty($data[1])) {
if($data[0] == 'Country') {
$results['country'] = ucwords(mb_strtolower($data[1]));
}
elseif($data[0] == 'City') {
$region = explode(',',$data[1]);
if(count($region) > 0) {
if(!empty($region[1])) {
$results['region'] = ucwords(mb_strtolower($region[1]));
}
if(!empty($region[0])) {
$results['city'] = ucwords(mb_strtolower($region[0]));
}
}else{
$results['city'] = ucwords(mb_strtolower($data[1]));
}
}
elseif($data[0] == 'Latitude') {
if(!empty($data[1])) {
$results['lat'] = $data[1];
}
}
elseif($data[0] == 'Longitude') {
if(!empty($data[1])) {
$results['lng'] = $data[1];
}
}
elseif($data[0] == 'IP') {
if(!empty($data[1])) {
$results['ip'] = $data[1];
}
}
}
}
continue;
}
if(empty($results['lat']) && empty($results['lng'])) {
return false;
}else{
$results['status'] = 'success';
return $results;
}
}else{
return false;
}
}
// All the different requests
function yahoo_request($location) {
// TODO: Update to new API:
// http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC&appid=
//$url = 'http://local.yahooapis.com/MapsService/V1/geocode?appid=';
$url = 'http://where.yahooapis.com/geocode?q=';
$url .= $location;
$url .= '&appid='.$this->settings['yahoo_api_key'];
return $url;
}
function google_request($location) {
$url = 'http://maps.google.com/maps/geo?q=';
$url .= $location;
$url .= "&output=xml&key=".$this->settings['google_api_key'];
$url .= "&oe=utf-8";
return $url;
}
function geonames_request($location) {
$url = "http://ws.geonames.org/postalCodeSearch?placename=";
$url .= $location;
$url .= "&&maxRows=3";
return $url;
}
function geocoder_us_request($location) {
// Untested, probably requires rewrite
$url = "http://geocoder.us/service/csv/geocode?address=";
$url .= $location;
return $url;
}
function geocoder_ca_request($location) {
$url = "http://geocoder.ca?locate=";
$url .= $location;
$url .= '&geoit=xml&standard=1';
return $url;
}
// Parse a Google response into something stamdarduseful
function parse_google_response($response) {
$result = array();
$response_array = xml2array($response);
if(empty($response_array['kml']['Response']['Status']) || $response_array['kml']['Response']['Status']['code'] != 200) {
return false;
}
// Get the lat and lng
$short_array = $response_array['kml']['Response']['Placemark'];
if(!empty($short_array['Point']['coordinates'])) {
$latlng = explode(',',$short_array['Point']['coordinates']);
$result['lng'] = $latlng[0];
$result['lat'] = $latlng[1];
}else{
$result['lat'] = false;
$result['lng'] = false;
}
// Get the city (needs refactor for readability)
if(!empty($short_array['AddressDetails']['Country']['AdministrativeArea']['Locality']['LocalityName'])
|| !empty($short_array['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'])) {
if(isset($short_array['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'])) {
$result['city'] = $short_array['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'];
}else{
$result['city'] = $short_array['AddressDetails']['Country']['AdministrativeArea']['Locality']['LocalityName'];
}
if(!empty($result['city'])) {
$result['city'] = ucwords(mb_strtolower($result['city']));
}
}else{
$result['city'] = '';
}
// Get the region name
if(!empty($short_array['AddressDetails']['Country']['AdministrativeArea']['AdministrativeAreaName'])) {
$result['region'] = $short_array['AddressDetails']['Country']['AdministrativeArea']['AdministrativeAreaName'];
}else{
$result['region'] = '';
}
// Get the country name
if(!empty($short_array['AddressDetails']['Country']['CountryNameCode'])) {
$result['country'] = $short_array['AddressDetails']['Country']['CountryNameCode'];
}else{
$result['country'] = '';
}
// Set the status
if(empty($result['lat']) || empty($result['lng'])) {
$result['status'] = 'fail';
}else{
$result['status'] = 'success';
}
$result['service'] = 'google';
return $result;
}
// Deals with a yahoo response
function parse_yahoo_response($response) {
$result = array();
$array_data = xml2array($response);
$result['type'] = 'yahoo';
if(empty($array_data) || $array_data['ResultSet']['Error'] == 1) {
return array('status'=>'fail');
}else{
if(!empty($array_data['ResultSet']['Result']['latitude']) && !empty($array_data['ResultSet']['Result']['longitude'])) {
$result['lat'] = $array_data['ResultSet']['Result']['latitude'];
$result['lng'] = $array_data['ResultSet']['Result']['longitude'];
}else{
$result['lat'] = null;
$result['lng'] = null;
}
if(!empty($array_data['ResultSet']['Result']['city'])) {
$result['city'] = $array_data['ResultSet']['Result']['city'];
}
if(!empty($array_data['ResultSet']['Result']['State'])) {
$result['region'] = $array_data['ResultSet']['Result']['state'];
}
if(!empty($array_data['ResultSet']['Result']['Country'])) {
$result['country'] = $array_data['ResultSet']['Result']['country'];
}
if(empty($result['lat']) || empty($result['lng'])) {
$result['status'] = 'fail';
}else{
$result['status'] = 'success';
}
}
return $result;
}
// Deals with the geonames response - not working properly at the moment
function parse_geonames_response($response) {
$array_data = xml2array($response);
$result['type'] = 'geonames';
if(empty($array_data['Geonames']['Code'][0])) {
return array('status'=>false);
}else{
if(!empty($array_data['Geonames']['Code'][0]['lat']) && !empty($array_data['Geonames']['Code'][0]['lng'])) {
$result['lat'] = $array_data['Geonames']['Code'][0]['lat'];
$result['lng'] = $array_data['Geonames']['Code'][0]['lng'];
}
if(!empty($array_data['Geonames']['Code'][0]['name'])) {
$result['city'] = $array_data['Geonames']['Code'][0]['name'];
}
if(!empty($array_data['Geonames']['Code'][0]['adminName1'])) {
$result['region'] = $array_data['Geonames']['Code'][0]['adminName1'];
}
if(!empty($array_data['Geonames']['Code'][0]['countryCode'])) {
$result['country'] = $array_data['Geonames']['Code'][0]['countryCode'];
}
$result['status'] = 'success';
}
return $result;
}
// Handles the geocoder_ca response
function parse_geocoder_ca_response($response) {
$array_data = xml2array($response);
$result['type'] = 'geocoder_ca';
if(empty($array_data) || isset($array_data['Geodata']['Error'])) {
return array('status'=>'fail');
}else{
if(!empty($array_data['Geodata']['latt']) && !empty($array_data['Geodata']['longt'])) {
$result['lat'] = $array_data['geodata']['latt'];
$result['lng'] = $array_data['geodata']['longt'];
}else{
$result['lat'] = null;
$result['lng'] = null;
}
if(!empty($array_data['geodata']['standard']['city'])) {
$result['city'] = ucwords(mb_strtolower($array_data['geodata']['standard']['city']));
}
if(!empty($array_data['Geodata']['standard']['prov'])) {
$result['region'] = ucfirst(mb_strtolower($array_data['geodata']['standard']['prov']));
}
$result['country'] = 'CA'; // It's geocoder_ca and will always be Canada
$result['status'] = 'success';
}
return $result;
}
}
?>