如何使用 Yelp API 从 v2 迁移到 v3?

How to migrate with Yelp API from v2 to v3?

我有一个脚本在 v2 上运行良好,但在过期并转移到 v3 时损坏了。

我已经尝试修复它,但很明显,除了将 v2 更改为 v3 之外,还有更多问题。显然他们已经弃用了秘密令牌。

这是我目前拥有的:

// 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>';

我们将不胜感激向正确方向的推动。


我遇到一个错误:

stdClass Object ( [error] => stdClass Object ( [code] => TOKEN_MISSING [description] => An access token must be supplied in order to use this endpoint. ) )

我是否需要为 v3 重新生成我的 API 凭据?

您似乎在使用 OAuth,根据 V3 的 yelp 开发人员文档,他们已转向基于 API 密钥的身份验证。

Prior to December 7, 2017 the API used OAuth 2.0 to authenticate requests to the API. In an effort to simplify authentication, starting March 1, 2018 the API no longer uses OAuth 2.0 for requests and moved over to only API Keys.

您可以在 https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going

找到身份验证详细信息

Citate from your question: Do I need to re-generate my API credentials for v3?

不!您不需要 重新 生成您的 API 凭据,因为您不再需要它们。但是你需要生成一个新的——一个 API 密钥。

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.

但请注意, 之前,您从 API 密钥生成开始(参见上面的最后一个 link):

  1. 您必须在 yelp.com 之前登录。如果某人在那里没有帐户,那么他必须在那里注册并确认他的电子邮件地址。
  2. 必须启用浏览器中的 JavaScript。在其他情况下,您将被重定向到非常奇怪的异常页面。

Citate from your bounty description: Need a working example of Yelp API v3 returning result of search business by phone.


YelpAPIv3搜索业务返回结果示例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>";
?>

我已经测试过了,可以用。