I have a script that worked great with v2
but broke when it expired and shifted to v3
.
I've attempted to fix it but clearly there's more to it then just changing v2
to v3
. Apparently they've deprecated secret token.
Here's what I have at the moment:
// Enter the path that the oauth library is in relation to the php file
require_once ('../lib/OAuth.php');
// For example, request business with id 'the-waterboy-sacramento'
$unsigned_url = "https://api.yelp.com/v3/businesses/search?term=niks-italian-kitchen-bar-austin";
// Set your keys here
$consumer_key = "xxxxxxx";
$consumer_secret = "xxxxxxxxx";
$token = "xxxxxxxx";
$token_secret = "xxxxxxxxxxx";
// 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();
// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);
// Get the signed URL
$signed_url = $oauthrequest->to_url();
echo $signed_url;
// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);
// Handle Yelp response data
$response = json_decode($data);
// Print it for debugging
echo '<pre>';
print_r($response);
echo '</pre>';
A nudge in the right direction would be highly appreciated.
I'm getting an error:
stdClass Object ( [error] => stdClass Object ( [code] => TOKEN_MISSING [description] => An access token must be supplied in order to use this endpoint. ) )
Do I need to re-generate my API credentials for v3
?
Citate from your question: Do I need to re-generate my API credentials for v3?
No! You do not need to re-generate your API credentials, because you do not need they anymore. But you need to generate a new one – an API Key.
Citate from Yelp API v3 documentation:
... starting March 1, 2018 the API no longer uses OAuth 2.0 for requests and moved over to only API Keys.With API Keys the process to authenticate is:
- Get your API Key from the Manage App page.
- Put the API Key in the request header as
"Authorization: Bearer <YOUR API KEY>"
.And that is it! You no longer need to make a request to the token endpoint to get an access token. Your API Key does not expire like the access tokens used to, so you don't need to worry about generating new ones.
But note that before you start with API Key generating (see the last link above):
Citate from your bounty description: Need a working example of Yelp API v3 returning result of search business by phone.
<?php
// request business by phone number
$request_url = "https://api.yelp.com/v3/businesses/search/phone?phone=+14157492060";
/*
Search for businesses by phone number. It must start with + and include the country code, like +14157492060.
See also https://www.yelp.com/developers/documentation/v3/business_search_phone
Additionly you will see the response body example.
*/
// Your API key:
$api_key = "Your-API-key-GUID"; //replase this string with your API key.
// Send Yelp API call
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
"Authorization: Bearer ".$api_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);
// Handle Yelp response data
$response = json_decode($data);
// Test: get a business on last index number
echo $response->businesses[$response->total - 1]->location->city;
// Print it
$pretty_response = json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo "<pre>".$pretty_response."</pre>";
?>
I have tested it and it works.