使用 cURL 发布数据时“需要长度”
当向服务器提交帖子字符串时,我不断收到
错误。Length required
$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);
如果我注释掉 CURLOPT_POSTFIELDS
和 CURLOPT_POST
,一切都很好。
有什么建议吗?
编辑
当我添加此行时
curl_setopt($ch, CURLOPT_HEADER, array('Content-Type:application/x-www-form-urlencoded'));
,我在 Length required
之前看到此错误
HTTP/1.1 411 长度必需 内容类型:text/html 日期:2011 年 5 月 16 日星期一 10:20:54 GMT 连接:关闭 内容长度:24
I keep getting a <h1>Length required</h1>
error, when submitting a post string to a server.
$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_POSTFIELDS
and CURLOPT_POST
, everything is fine.
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
HTTP/1.1 411 Length Required Content-Type: text/html Date: Mon, 16 May 2011 10:20:54 GMT Connection: close Content-Length: 24
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的用法完全混乱和混乱。
不要自己更改 Content-Length,而是让 libcurl 这样做,以便它变得正确。
您打算进行多部分表单提交(将哈希数组传递给 POSTFIELDS 选项)还是“常规”表单(将字符串传递给 POSTFIELDS 选项)?接收者很可能会采用其中一种,而您不能随意选择一种类型。
当您有正确的 POST 并且您知道您正确发送了数据(验证记录的浏览器使用情况)时,然后您可以看到服务器所说的内容,如果它仍然坚持认为有问题,那么您回顾录制的会话并调整您的请求以使其更类似于该会话。冲洗并重复直到它起作用。
Your usage is completely mixed up and confused.
Don't change Content-Length yourself but let libcurl do it so that it gets correct.
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.
When you have the correct POST and you know you send the data correctly (verify against a recorded brower use), then you 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.
嗯,我没有看到您告诉 cURL 您的意图是执行 POST 请求。添加以下选项:
这可能会解决问题。
Ehrm, I don't see you telling cURL that your intent is to do a POST request. Add the following option:
That might fix the issue.
只是为了那些有相同症状的人而添加:
如果您要发布到 IIS 6 并且没有任何内容,您仍然需要发送 Content-Length: 0 否则它会抱怨“需要长度”。
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".
编辑:您是否也尝试删除 &放大器; ?
=============
您是否尝试注释掉
您的 cookie 数据是否有效?
尝试放置代理:
设置推荐人:
curl_setopt($ch, CURLOPT_REFERER,$url);
EDIT : Did you also try to remove the & amp; ?
=============
Did you try to comment out
And is your cookie data valid?
Try to put an agent in :
Set up a referrer :
curl_setopt($ch, CURLOPT_REFERER,$url);
CURLOPT_POST=>true
在curl中设置
CURLOPT_POST=>true
set in the curl