將提交後字符串提交給服務器時,我總是收到<h1>Length required</h1>
錯誤。'使用cURL發佈數據時'需要的長度'
$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長度必需的Content-Type:text/html的日期:2011年5月16日星期一10:20:54 GMT連接:關閉內容長度:24
有你嘗試過了「這不應該是必要的」取出來?因爲不,所以不需要。 – lonesomeday 2011-05-16 09:40:37
是的,它不會使所有 – Ragnar123 2011-05-16 09:42:29