PHP Curl请求返回HTTP错误411:请求必须被分块或具有内容长度

阿姆兰·杜塔(Amlan Dutta)

我正在尝试在PHP中发送以下CURL请求。但是它返回:“ HTTP错误411。请求必须分块或具有内容长度。”

包含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;
}
?>  

输出:

Length Required

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

由于我是使用PHP Curl的新手,所以我无法找出问题所在。如果有人可以通过示例向我简要介绍该解决方案,他将不胜感激。谢谢!

葡萄干

由于您的数据似乎是作为URL参数发送的,因此请尝试添加内容长度为零的标头,如下所示:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));

同样,不要触摸内容长度标题,只需尝试添加一个空的POST正文。根据要发布到的HTTP服务器的类型,其行为略有不同(IIS,LightHTTPD,Apache)。

空的帖子正文类似于:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array()));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章