I am running php 7.2.24 and testing the Yelp PHP sample have noticed that it produces no results but errors. The code was written 6 years ago, so not sure if it is an outdated code issue or perhaps using the wrong version of PHP.
Error is the following: Fatal error: Curl failed with error #400: {"error": {"code": "VALIDATION_ERROR", "description": "'Bearer' does not match '^(?i)Bearer [A-Za-z0-9\\-\\_]{128}$'", "field": "Authorization", "instance": "Bearer"}}
Yelp PHP code link -> https://github.com/Yelp/yelp-fusion/tree/master/fusion/php
Guidance is greatly appreciated.
Diego
====== MY CODE BELOW ======
<?php
$API_KEY = 'My_Yelp_API_Key';
// Complain if credentials haven't been filled out.
assert($API_KEY, "Please supply your API key.");
// API constants, you shouldn't have to change these.
$API_HOST = "https://api.yelp.com";
$SEARCH_PATH = "/v3/businesses/search";
$BUSINESS_PATH = "/v3/businesses/"; // Business ID will come after slash.
// Defaults for our simple example.
$DEFAULT_TERM = "dinner";
$DEFAULT_LOCATION = "San Francisco, CA";
$SEARCH_LIMIT = 3;
/**
* Makes a request to the Yelp API and returns the response
*
* @param $host The domain host of the API
* @param $path The path of the API after the domain.
* @param $url_params Array of query-string parameters.
* @return The JSON response from the request
*/
function request($host, $path, $url_params = array()) {
// Send Yelp API Call
try {
$curl = curl_init();
if (FALSE === $curl)
throw new Exception('Failed to initialize');
$url = $host . $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;
}
/**
* Query the Search API by a search term and location
*
* @param $term The search term passed to the API
* @param $location The search location passed to the API
* @return The JSON response from the request
*/
function search($term, $location) {
$url_params = array();
$url_params['term'] = $term;
$url_params['location'] = $location;
$url_params['limit'] = $GLOBALS['SEARCH_LIMIT'];
return request($GLOBALS['API_HOST'], $GLOBALS['SEARCH_PATH'], $url_params);
}
/**
* Query the Business API by business_id
*
* @param $business_id The ID of the business to query
* @return The JSON response from the request
*/
function get_business($business_id) {
$business_path = $GLOBALS['BUSINESS_PATH'] . urlencode($business_id);
return request($GLOBALS['API_HOST'], $business_path);
}
/**
* Queries the API by the input values from the user
*
* @param $term The search term to query
* @param $location The location of the business to query
*/
function query_api($term, $location) {
$response = json_decode(search($term, $location));
$business_id = $response->businesses[0]->id;
print sprintf(
"%d businesses found, querying business info for the top result \"%s\"\n\n",
count($response->businesses),
$business_id
);
$response = get_business($business_id);
print sprintf("Result for business \"%s\" found:\n", $business_id);
$pretty_response = json_encode(json_decode($response), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
print "$pretty_response\n";
}
/**
* User input is handled here
*/
$longopts = array(
"term::",
"location::",
);
$options = getopt("", $longopts);
$term = $options['term'] ?: $GLOBALS['DEFAULT_TERM'];
$location = $options['location'] ?: $GLOBALS['DEFAULT_LOCATION'];
query_api($term, $location);
?>