[英][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>';
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.