简体   繁体   English

[API]使用Yelp API / PHP

[英][API]Using Yelp API / PHP

First of all 首先

  • I want to use Yelp API with PHP. 我想将Yelp API与PHP结合使用。
  • I want to get bearer access token, but can't. 我想获取承载访问令牌,但是不能。

Code

The code below is one that I executed. 下面的代码是我执行的代码。

$url = "https://api.yelp.com/oauth2/token";
$clientEncode = urlencode("?grant_type=client_credentials?client_id=MY_CLIENT_ID?client_secret=MY_CLIENT_SECRET");
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientEncode);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$response = curl_exec($curl);
$array = json_decode($response, true);
$arrayVal = array_values($array);
$bearer_token = $array->access_token;
echo '<pre>';
var_dump($array);
echo '</pre>';

And, the message below is the error message. 并且,以下消息是错误消息。

/home/ubuntu/workspace/index.php:21:
class stdClass#1 (1) {
public $error =>
class stdClass#2 (2) {
public $code =>
string(16) "VALIDATION_ERROR"
public $description =>
string(165) "client_id or client_secret parameters not found. Make sure to provide client_id and client_secret in the body with the application/x-www-form-urlencoded content-type"
}
}

Maybe, the use of cURL is Okay, I think, but I don't know how to add parameters on the url. 我想也许可以使用cURL,但是我不知道如何在URL上添加参数。

Please teach me more good practice. 请教我更多好的做法。

Thanks. 谢谢。

I Hope This will work for you :) 我希望这对您有用:)

<?
    $clientEncode = urlencode("https://api.yelp.com/oauth2/token?client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $clientEncode);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
    $response = curl_exec($curl);
    $array = json_decode($response, true);
    $arrayVal = array_values($array);
    $bearer_token = $array->access_token;
    echo '<pre>';
    var_dump($array);
    echo '</pre>';

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM