-
Notifications
You must be signed in to change notification settings - Fork 0
/
food_truck_data.php
94 lines (87 loc) · 2.75 KB
/
food_truck_data.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
<?php
require_once('oauth.php');
function fetch_data($uri) {
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Fetch SF food truck data.
function get_food_trucks() {
return fetch_data('http://data.sfgov.org/resource/rqzj-sfat.json');
}
// Use Yelp data to guess food truck hours.
function get_yelp_data_for_truck($vendor_name, $lat, $long) {
// Configuration.
$consumer_key = '';
$consumer_secret = '';
$token = '';
$token_secret = '';
// Search params.
$params = array(
'term' => $vendor_name,
'category_filter' => 'foodtrucks,foodstands',
'location' => 'San Francisco, CA',
'cll' => (string)$lat.",".(string)$long,
'limit' => 1, // For simiplicity's sake, get best match only.
);
// Build the request.
$unsigned_uri = "http://api.yelp.com/v2/search/?" . http_build_query($params);
// Token object built using the OAuth library
$token = new OAuthToken($token, $token_secret);
// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauthrequest = OAuthRequest::from_consumer_and_token(
$consumer,
$token,
'GET',
$unsigned_uri
);
// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);
// Get the signed URL
$signed_url = $oauthrequest->to_url();
$results = fetch_data($signed_url);
// Ensure a business listing is returned and the location is not closed
// permanently.
if (array_key_exists("businesses", $results) &&
!$results["businesses"][0]["is_closed"]) {
return $results["businesses"][0];
}
return null;
}
function get_food_truck_data() {
$food_trucks_src = get_food_trucks();
// Only keep food truck data we care about.
$food_trucks = array();
foreach ($food_trucks_src as $fts) {
if ($fts["status"] === "APPROVED") {
$yelp_data = get_yelp_data_for_truck(
$fts["applicant"],
$fts["latitude"],
$fts["longitude"]
);
// If this truck has a yelp listing, it should actually exist :-)
if ($yelp_data) {
$food_trucks[$fts["applicant"]] = array(
"vendorname" => $yelp_data["name"],
"fooditems" => $fts["fooditems"],
"latitude" => $fts["latitude"],
"longitude" => $fts["longitude"],
"thumbnail" => $yelp_data["image_url"],
"phone" => $yelp_data["phone"],
"displayphone" => $yelp_data["display_phone"],
"yelpurl" => $yelp_data["url"],
);
}
}
}
return $food_trucks;
}
$food_truck_data = get_food_truck_data();
echo json_encode($food_truck_data);
?>