php “需要长度”,当使用 cURL 发布数据时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6015501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:09:34  来源:igfitidea点击:

'Length required', when posting data with cURL

phpcurlhttp-posthttp-error

提问by Ragnar123

I keep getting a <h1>Length required</h1>error, when submitting a post string to a server.

<h1>Length required</h1>向服务器提交帖子字符串时,我不断收到错误消息。

$cookie = "Secret cookie data here";

$searchData = array(
        '__EVENTTARGET' => 'ctl00$main$btn',
        'ctl00$main$tbMNr' => $_GET['smth'],
        'ctl00$main$tbMb' => $_GET['smthElse'],
        '__VIEWSTATE' => 'smthElseHere'
);

// Commenting this out, as suggested by user lonesomeday
//foreach ($searchData as &$elem) // This should not be necessary
//    $elem = urlencode($elem);


// create a new cURL resource

$fields = http_build_query($searchData); // Assuming it's an array

if ($ch = curl_init("http://mysite.com"))
{
        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_POST, true); // Suggestion from Berry Langerak - no difference, same error
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}



$result = curl_exec($ch);
if ($result === false) {
    echo 'Curl error: ' . curl_error($ch);
}
echo "<pre>".$fields."</pre>".$result; // For debugging ONLY

curl_close($ch);

If I comment out the CURLOPT_POSTFIELDSand CURLOPT_POST, everything is fine.

如果我注释掉CURLOPT_POSTFIELDSand CURLOPT_POST,一切都很好。

Any suggestions?

有什么建议?

Edit

编辑

When I add this line

当我添加这一行

curl_setopt($ch, CURLOPT_HEADER, array('Content-Type:application/x-www-form-urlencoded'));

I see this error, right before Length Required

我看到这个错误,就在之前 Length Required

HTTP/1.1 411 Length Required Content-Type: text/html Date: Mon, 16 May 2011 10:20:54 GMT Connection: close Content-Length: 24

HTTP/1.1 411 长度要求内容类型:text/html 日期:2011 年 5 月 16 日星期一 10:20:54 GMT 连接:关闭内容长度:24

采纳答案by Daniel Stenberg

Your usage is completely mixed up and confused.

您的用法完全混乱和混乱。

  1. Don't change Content-Length yourself but let libcurl do it so that it gets correct.

  2. Are you intending to do a multipart formpost (pass a hash array to the POSTFIELDS option) or a "regular" one (pass a string to the POSTFIELDS option) ? The receiver will mostly likely assume one of them and you can't just randomly select a type at your own will.

  3. When you have the correct POST and you know you send the data correctly (verify against a recorded brower use), thenyou can see what the server says and if it still insists something is wrong then you look back on the recorded session and adjust your request to be more similar to that. Rinse and repeat until it works.

  1. 不要自己更改 Content-Length 而是让 libcurl 来做,这样它就正确了。

  2. 您打算进行多部分表单发布(将哈希数组传递给 POSTFIELDS 选项)还是“常规”表单(将字符串传递给 POSTFIELDS 选项)?接收者很可能会假设其中一个,您不能随意随机选择一种类型。

  3. 当您拥有正确的 POST 并且您知道您正确发送了数据(根据记录的浏览器使用情况进行验证),然后您可以看到服务器说了什么,如果它仍然坚持认为有问题,那么您回顾记录的会话并调整您的要求更相似。冲洗并重复,直到它起作用为止。

回答by MetaGuru

Just wanted to add for the sake of people who came here with the same symptoms:

只想为有同样症状的人补充:

If you are POSTing to IIS 6 and do not have any Content, you still need to send Content-Length: 0 or it will complain "Length Required".

如果您正在发布到 IIS 6 并且没有任何内容,您仍然需要发送 Content-Length: 0 否则它会抱怨“需要长度”。

回答by Berry Langerak

Ehrm, I don't see you telling cURL that your intent is to do a POST request. Add the following option:

Ehrm,我没有看到您告诉 cURL 您的意图是执行 POST 请求。添加以下选项:

curl_setopt($ch, CURLOPT_POST, true);

That might fix the issue.

这可能会解决问题。

回答by Paolo_Mulder

EDIT: Did you also try to remove the & amp; ?

编辑:您是否也尝试删除 & amp; ?

http_build_query($array, '', '&');

=============

==============

Did you try to comment out

你有没有尝试注释掉

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields)));

And is your cookie data valid?

您的 cookie 数据是否有效?

Try to put an agent in :

尝试将代理放入:

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  • Set up a referrer :

    curl_setopt($ch, CURLOPT_REFERER,$url);

  • 设置推荐人:

    curl_setopt($ch, CURLOPT_REFERER,$url);

回答by mahesh

$query ="email=".urlencode($usernames[$key]."@sktechno.net")."&firstName=".urlencode($usernames[$key])."&lastName=".urlencode($lastnames[$key]);

$headerX = array(
'ISV_API_KEY: f6c1c52d03f49c9b4f94150256f2f0dcec6ada1d175d06e990e2d692f443a2db',
'ISV_API_SECRET: 8ae599feda50db968cf4b536e7d79e4ad2d6bd60f41715336b73002819795cc0'
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerX);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);