php - 为什么 PHP curl 不起作用?当它从命令行 curl 工作时?

标签 php curl centos

当我从命令行执行以下操作时,它可以工作:

$ 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 编码数据。

关于php - 为什么 PHP curl 不起作用?当它从命令行 curl 工作时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56257402/

相关文章:

php - Nanopb - 用 PHP 解码

delphi - CURL 作为纯 URL 字符串

php - 使用 CURL 检查网站是否使用 SSL

linux - 在 CentOS 上永久安全地删除文件

javascript - 重新加载时从 url 获取 php 变量而不刷新

php - 正则表达式 PHP 仅在未被引号包围时才匹配

php - 使用 PHP 检测历史变化?

rest - 使用 REST 将数据保存到 Firebase

r - 在 CentOS 的 fftwtools 上安装 R 包需要 fftw3

linux - Linux 中文件目录的并集