-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestaurants.php
64 lines (51 loc) · 2.14 KB
/
restaurants.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
<?php
function getRestaurants($region){
$API_KEY = '-7nfonCOnXQYc8aFYuYjV8b6IJqtxgeu88MufQf3WSeWaozRpg9nIHAQxR-R30dWEnv0IY-4vQKVmr5Fq8EY28A-iBmWOMU2E8BipBUz_Cn0VHHaYmtnPrzPBg_-W3Yx';
// Complain if credentials haven't been filled out.
assert($API_KEY, "Please supply your API key.");
// API constants
$API_HOST = "https://api.yelp.com";
$SEARCH_PATH = "/v3/businesses/search";
$BUSINESS_PATH = "/v3/businesses/"; // Business ID will come after slash.
$url_params = array();
$url_params['term'] = 'restaurants';
$url_params['location'] = $region;
$url_params['limit'] = 10;
// Send Yelp API Call
try {
$curl = curl_init();
if (FALSE === $curl){
throw new Exception('Failed to initialize');
}
$url = $API_HOST . $SEARCH_PATH . "?" . http_build_query($url_params);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true, // Capture response.
CURLOPT_ENCODING => "", // Accept gzip/deflate/whatever.
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $GLOBALS['API_KEY'],
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
if (FALSE === $response){
throw new Exception(curl_error($curl), curl_errno($curl));
}
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $http_status){
throw new Exception($response, $http_status);
}
curl_close($curl);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
return $response;
}
?>