当我从命令行执行以下操作时,它会起作用:
$ curl -X "POST" "https://ABCD/login/oauth2/access_token" -H "Authorization: Basic XXXX=" -H "Content-Type:application/x-www-form-urlencoded" --data-urlencode "realm=XXX" --data-urlencode "XXX=XXX"
但是当我从PHP进行操作时,它不起作用:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ABCD/login/oauth2/access_token');
curl_setopt($ch, CURLOPT_POST, 1);
$ss = array(
'realm' => 'XXX',
'XXX'=>'XXX'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Authorization: Basic XXXX=',
'Content-Type: application/x-www-form-urlencoded',
));
$result=curl_exec ($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
有人知道我做错了什么吗?我的HTTP状态为“必需的参数或正文丢失或不正确。”
答案 0 :(得分:2)
您的命令行显示--data-urlencode
-URL编码
您的PHP说'Content-Type: application/x-www-form-urlencoded',
-所以您说您是对数据进行URL编码
它还说curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss));
—所以您是 JSON编码,而不是URL编码。
发送您声称要发送的URL编码数据。