Rag*_*123 9 php curl http-post http-error
<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);
Run Code Online (Sandbox Code Playgroud)
如果我注释掉的CURLOPT_POSTFIELDS和CURLOPT_POST,一切都很好.
有什么建议?
编辑
当我添加这一行
curl_setopt($ch, CURLOPT_HEADER, array('Content-Type:application/x-www-form-urlencoded'));
Run Code Online (Sandbox Code Playgroud)
我之前看到了这个错误 Length Required
HTTP/1.1 411所需长度内容类型:text/html日期:星期一,2011年5月16日10:20:54 GMT连接:关闭内容长度:24
您的使用完全混淆和困惑.
不要自己更改Content-Length,但让libcurl执行它以使其正确.
您打算执行multipart formpost(将哈希数组传递给POSTFIELDS选项)还是"常规"(将字符串传递给POSTFIELDS选项)?接收器很可能会假设其中一个,你不能随意随意选择一种类型.
如果你有正确的POST,并且你知道你正确地发送了数据(根据记录的浏览器使用进行验证),那么你可以看到服务器说的是什么,如果它仍然坚持有问题,那么你回顾一下录制的会话并调整你的请求更相似.冲洗并重复直至其工作.