【发布时间】: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。