PHP Curl 请求返回 HTTP 错误 411:请求必须分块或具有内容长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46516283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Curl Request returning HTTP Error 411: The request must be chunked or have a content length
提问by Amlan Dutta
I'm trying to send the following CURL request in PHP. But its returning: "HTTP Error 411. The request must be chunked or have a content length."
我正在尝试在 PHP 中发送以下 CURL 请求。但它返回:“HTTP 错误 411。请求必须分块或具有内容长度。”
PHP Script containing the CURL Request:
包含 CURL 请求的 PHP 脚本:
<?php
$numbers = 9999999999;
$message = "Test";
$message = urlencode($message);
$port = 80;
$url = "http://191.95.51.64/API/sendsms.aspx?loginID=myloginid&password=mypassword&mobile=".$numbers."&text=".$message."&senderid=ABCDEF&route_id=1&Unicode=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_PORT, $port );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo $err;
} else {
echo $output;
}
?>
Output:
输出:
Length Required
HTTP Error 411. The request must be chunked or have a content length.
Since I'm new to use PHP Curl, so I couldn't be able to find out whats wrong. If anyone can briefly guide me the solution with an example, I'll be very appreciated to him. Thanks!
由于我是使用 PHP Curl 的新手,所以我无法找出问题所在。如果有人可以通过示例简要指导我解决方案,我将非常感谢他。谢谢!
回答by raidenace
Since your data seems to be send as URL parameters, try adding content length header of zero like below:
由于您的数据似乎作为 URL 参数发送,请尝试添加零内容长度标头,如下所示:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
Also instead of touching the content length header, simply try to add an empty POST body. Based on which type of HTTP server you are posting to, the behaviour differs slightly (IIS, LightHTTPD, Apache).
此外,不要触及内容长度标题,只需尝试添加一个空的 POST 正文即可。根据您发布到哪种类型的 HTTP 服务器,行为略有不同(IIS、LightHTTPD、Apache)。
Empty post body similar to:
类似于以下内容的空帖子正文:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array()));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array()));
回答by Neal Garrett
You forgot to close quote:
你忘了关闭报价:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));