CURL Post 请求产生错误 '{"error":"invalid_request"}' (length=27)
CURL Post request yields in error '{"error":"invalid_request"}' (length=27)
CURL Post 请求产生错误 '{"error":"invalid_request"}' (length=27)
这是我的代码
$url="https://...................";
$authorization=""; //base64 string TE1TQXBpOkxNUContinue...=
$curl = curl_init();
$auth_data = array(
'scope' => 'LMSApi LMSRead',
'grant_type' => 'client_credentials'
);
$ops=array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS=>$auth_data,
CURLOPT_HTTPAUTH=> CURLAUTH_BASIC,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Content-Type'=> 'application/x-www-form-urlencode',
'Authorization'=> 'Basic '.$authorization
)
);
curl_setopt_array($curl,$ops);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
当使用 postman 时它可以工作,但是 returns 在使用上面的代码时总是错误的。不知道我在做什么 wrong.curl_error($ch) 结果为空字符串
需要帮助
如果有人遇到同样的问题,以下是解决方法。
将 'grant_type=client_credentials&scope=specify scope' 直接传递给 CURLOPT_POSTFIELDS 而不是数组解决了我的问题。现在的代码如下所示。
$url="https://something.com";
$headerpost=array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic yourbase64string'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=LMSApi LMSRead');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headerpost);
$result = curl_exec($ch);
$jsonresult=json_decode($result,true);
curl_close($ch);
CURL Post 请求产生错误 '{"error":"invalid_request"}' (length=27) 这是我的代码
$url="https://...................";
$authorization=""; //base64 string TE1TQXBpOkxNUContinue...=
$curl = curl_init();
$auth_data = array(
'scope' => 'LMSApi LMSRead',
'grant_type' => 'client_credentials'
);
$ops=array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS=>$auth_data,
CURLOPT_HTTPAUTH=> CURLAUTH_BASIC,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Content-Type'=> 'application/x-www-form-urlencode',
'Authorization'=> 'Basic '.$authorization
)
);
curl_setopt_array($curl,$ops);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
当使用 postman 时它可以工作,但是 returns 在使用上面的代码时总是错误的。不知道我在做什么 wrong.curl_error($ch) 结果为空字符串
需要帮助
如果有人遇到同样的问题,以下是解决方法。
将 'grant_type=client_credentials&scope=specify scope' 直接传递给 CURLOPT_POSTFIELDS 而不是数组解决了我的问题。现在的代码如下所示。
$url="https://something.com";
$headerpost=array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic yourbase64string'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=LMSApi LMSRead');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headerpost);
$result = curl_exec($ch);
$jsonresult=json_decode($result,true);
curl_close($ch);