forked from straup/php-lib-enplacify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_enplacify_foodspotting.php
136 lines (95 loc) · 2.86 KB
/
lib_enplacify_foodspotting.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
<?php
#
# $Id$
#
loadlib("vcard");
loadlib("geo_geocode");
######################################################
function enplacify_foodspotting_uri($uri){
$place_id = enplacify_foodspotting_uri_to_id($uri);
if (! $place_id){
return array(
'ok' => 0,
'error' => 'failed to recognize places id',
'url' => $url,
);
}
$rsp = enplacify_foodspotting_get_place($place_id);
if (! $rsp['ok']){
return $rsp;
}
$place = array(
'name' => $rsp['place']['fn org'],
'derived_from' => 'foodspotting',
'derived_from_id' => $rsp['place']['id'],
);
$others = array(
'latitude' => 'latitude',
'longitude' => 'longitude',
'street-address' => 'address',
'tel' => 'phone',
);
foreach ($others as $theirs => $ours){
if (isset($rsp['place'][$theirs])){
$place[$ours] = $rsp['place'][$theirs];
}
}
return array(
'ok' => 1,
'place' => $place,
);
}
######################################################
function enplacify_foodspotting_uri_to_id($uri){
return enplacify_service_uri_to_id('foodspotting', $uri);
}
######################################################
function enplacify_foodspotting_get_place($place_id){
$cache_key = "enplacify_foodspotting_place_{$place_id}";
$cache = cache_get($cache_key);
if ($cache['ok']){
return $cache['data'];
}
$url = "http://www.foodspotting.com/places/" . urlencode($place_id);
$http_rsp = http_get($url);
if (! $http_rsp['ok']){
return $http_rsp;
}
$rsp = vcard_parse_html($http_rsp['body']);
if ($rsp['ok']){
$place = $rsp['vcard'];
$place['id'] = $place_id;
# vcard has no specifics for latlon so just assume this is false and look for:
# <input id="place_latitude" name="place[latitude]" type="hidden" value="35.6633801" />
# <input id="place_longitude" name="place[longitude]" type="hidden" value="139.71029
$has_latlon = 0;
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$html = mb_convert_encoding($http_rsp['body'], 'html-entities', 'utf-8');
if ($doc->loadHTML($html)){
foreach ($doc->getElementsByTagName('input') as $i){
$id = $i->getAttribute('id');
if (preg_match("/place_(latitude|longitude)/", $id, $m)){
$place[ $m[1] ] = $i->getAttribute('value');
}
}
$has_latlon = ($place['latitude'] && $place['longitude']) ? 1 : 0;
}
if ((! $has_latlon) && ($place['street-address'] && $place['locality'] && $place['region'])){
$q = "{$place['street-address']}, {$place['locality']} {$place['region']}";
$geo_rsp = geo_geocode_string($q);
if ($geo_rsp['ok']){
$place['latitude'] = $geo_rsp['latitude'];
$place['longitude'] = $geo_rsp['longitude'];
}
}
$rsp = array(
'ok' => 1,
'place' => $place,
);
}
cache_set($cache_key, $rsp);
return $rsp;
}
######################################################
?>