PHP CURL:HTTP/1.1 411 长度要求

PHP CURL: HTTP/1.1 411 Length Required

即使我指定了内容长度,我仍然收到错误:

Length Required


HTTP Error 411. The request must be chunked or have a content length.

这是我的代码

$curl = curl_init();
$data = array("Key" => "Value", "Key2" => "Value2");
$data = json_encode($data);
$length = strlen($data);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json;charset=utf-8", "Content-Length: $length"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, true);

$result = curl_exec($curl);

有什么想法吗?

您将 Content-length 设置为 $data 的长度,但是您 post $update_json 而不是 $data

您确定需要使用 NTLM 身份验证选项吗?

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);

我对此进行了测试,它导致 content-type header 以及 JSON 负载从请求中省略。

尝试删除该行并查看请求是否有效。

我遇到了同样的问题,并且为此绞尽脑汁几个小时。 只需从 header 中删除 content-length:

像这样

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json;charset=utf-8"))