【问题标题】:PHP cURL with JSON and raw body [duplicate]带有 JSON 和原始正文的 PHP cURL [重复]
【发布时间】:2018-02-02 20:29:06
【问题描述】:

我正在尝试将 API 与 PHP 连接起来。但我得到的只是“需要命令参数”。为了工作,我需要用它发送 RAW Body 数据,并且它需要在 PUT 方法中。

Json 数据需要看起来像

{
"OutputID":"Some key",
"Activate":true,
}

这是我的代码

    $curl = curl_init($URL);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");  

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

$jsonData = array(
    'OutputID' => 'Some key',
    'Activate' => true,
);

$jsonDataEncoded = json_encode($jsonData);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonDataEncoded);

curl_setopt($curl, CURLOPT_HTTPHEADER, array(   
    'Content-Type: application/json',
    'Content-Length: 0',
    $Authorization_Token_Key));


if(curl_exec($curl) === false)
{
    echo 'Curl error: ' . curl_error($curl);
}
else
{
    echo '';
}

如果我用 Postman 尝试它,它可以工作,但不能在 PHP 脚本中。

【问题讨论】:

  • 您能否详细说明它在 PHP 中不起作用的意思?
  • 既然要使用PUT方法,我想你应该去掉curl_setopt($curl, CURLOPT_POST, true);
  • 你的Content-Length 不是0。

标签: php json curl postman


【解决方案1】:

@帕特里克

你在哪里对:)

我删除了

curl_setopt($curl, CURLOPT_POST, true);

它就像一个魅力

谢谢

【讨论】:

猜你喜欢
  • 2015-07-25
  • 2015-12-02
  • 2020-01-02
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多