我正在尝试获取访问令牌

时间:2019-09-25 06:44:08

标签: php rest api curl

$curl = curl_init();
$auth_data = array(
 'client_id'        => 'xxxxxxxx', // i have it
 'client_secret'    => 'xxxxxxxx', // I have it
 'grant_type'       => 'client_credentials'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, 'https://xxxxxx.caspio.com/oauth/token'); // I have it
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;

我想知道为什么我得到Bad Request。当我尝试显示错误时,什么也没有显示。

1 个答案:

答案 0 :(得分:1)

这对我有用

$curl = curl_init();

curl_setopt_array( $curl, array(
    CURLOPT_URL => "<TOKEN ENDPOINT URL>",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS =>"grant_type=client_credentials&client_id=<client_ID>&client_secret=<Secret>",
));

$response = curl_exec($curl);