'Length required', when posting data with cURL

I keep getting a

Length required

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

9
задан Ragnar123 16 May 2011 в 18:57
поделиться