[API]使用 Yelp API / PHP

[API]Using Yelp API / PHP

首先

代码

下面的代码是我执行的。

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

而且,下面的消息是错误消息。

/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"
}
}

也许,使用cURL是可以的,我想,但我不知道如何在url上添加参数。

请教我更多好的做法。

谢谢。

我希望这对你有用:)

<?
    $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>';

    ?>