gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:39:50 26 4
gpt4 key购买 nike

我有一个脚本在 v2 上工作得很好,但在它过期并转移到 v3 时就坏了。

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

这是我目前拥有的:

// 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 凭据?

最佳答案

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

不!您不需要重新-生成您的 API 凭据,因为您不再需要它们。但是您需要生成一个新的 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.

但请注意,之前您开始生成 API key (请参阅上面的最后一个链接):

  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.


Yelp API v3 电话搜索业务返回结果示例

<?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>";
?>

我已经测试过它并且有效。

关于php - 如何使用 Yelp API 从 v2 迁移到 v3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52105374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com