繁体   English   中英

为什么PHP curl无法正常工作? 从命令行curl何时起作用?

[英]Why the PHP curl is not working? When it works from command line curl?

当我从命令行执行以下操作时:

$ 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',因此您您是对数据进行URL编码

它还说curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss)); —因此您使用JSON编码而不是URL编码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM