为什么PHP卷发不起作用?当它从命令行卷发起作用时



当我从命令行进行操作时,它有效:

$ 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状态,"所需的参数或身体丢失或不正确。"

您的命令行说--data-urlencode - URL编码

您的php说'Content-Type: application/x-www-form-urlencoded', - 所以您您正在编码数据

它也说curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss)); - 所以您是 JSON编码 it而不是编码URL。

发送您声称要发送的URL编码数据。

最新更新