'Length required', when posting data with cURL
Asked Answered
L

6

10

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

Larcener answered 16/5, 2011 at 9:38 Comment(2)
Have you tried it with the "this should not be necessary" taken out? Because no, it shouldn't be necessary.Brott
Yes, it does not make any difference at allLarcener
M
6

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), 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.

Mazy answered 16/5, 2011 at 12:15 Comment(2)
1. I did not want to change content-length - I speified it, hoping it would help my issue. I want to use an regular one with the string. Now I've implemented it using http_build_query($searchData) It seems like I am not able to use POST and Cookies at the same time with curl, see this thread, I created afterwards, maybe narrowing the issue down: #6019794.Larcener
Oh well, the problem was when I loaded the cookie.Larcener
Z
4

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

curl_setopt($ch, CURLOPT_POST, true);

That might fix the issue.

Zebadiah answered 16/5, 2011 at 9:42 Comment(2)
I tried, it does not seem to work. Does the order of the parameters make any difference?Larcener
Remove the content-length line, and see if that makes any difference? If you supply an incorrect value, I can imagine the server doesn't like you for that.Zebadiah
I
4

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".

Isaiah answered 28/7, 2011 at 18:49 Comment(0)
F
1

EDIT : Did you also try to remove the & 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?

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);

Fotinas answered 16/5, 2011 at 10:29 Comment(2)
Are all your fields valid ? Did you try it with just a simple request ? test=test&test2=test2Fotinas
Yes, I have tried simple requests. I dont think the server gets any requests, after I have sent the cookie.Larcener
M
0
$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);
Microclimate answered 8/11, 2013 at 10:1 Comment(0)
C
0

CURLOPT_POST=>true

set in the curl

Chinookan answered 7/2 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.