Help with CURL 411 Length Required

Hello Masters,

My curl code is written below, but it results in 411 Length Required.
Note that I tested with

$hdr[] = "Content-Length: ".$bodyLength."\\r\
\\r\
";

but that didnt make any difference, thats why I commented that line off.


$hdr = array();
$hdr[] = "POST /abc/mypage.aspx HTTP/1.1\\r\
";
$hdr[] = "Host: mysite.com\\r\
";
$hdr[] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0\\r\
";
$hdr[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\\r\
";
$hdr[] = "Accept-Language: en-US,en;q=0.5\\r\
";
$hdr[] = "Accept-Encoding: gzip, deflate\\r\
";
$hdr[] = "Connection: keep-alive\\r\
";
$hdr[] = "Referer: https://www.mysite.com/abc/mypage.aspx\\r\
";
$hdr[] = "Cookie: ASP.NET_SessionId=$ASPSESS\\r\
";
$hdr[] = "Content-Type: multipart/form-data; boundry=".$d.$boundary."\\r\
";
//$hdr[] = "Content-Length: ".$bodyLength."\\r\
\\r\
";

		
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_URL, "https://www.mysite.com/abc/mypage.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $hdr);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");
$data = curl_exec($ch);
curl_close($ch);

Please help
Regards
ZH

How are you defining $bodyLength ?

Here it is how:

$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="__VIEWSTATE"'.CRLF.CRLF.$__VIEWSTATE;
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="tbxFirstName"'.CRLF.CRLF.$tbxFirstName;	
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="tbxLastName"'.CRLF.CRLF.$tbxLastName;	
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="tbxAge"'.CRLF.CRLF.$tbxAge;	
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="tbxPatientMRNo"'.CRLF.CRLF.$tbxPatientMRNo;	
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="rlstSex"'.CRLF.CRLF.$rlstSex;	
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="FindFile"; filename=""'.CRLF.$CTOS.CRLF.CRLF;
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="File1"; filename=""'.CRLF.$CTOS.CRLF.CRLF;
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="File2"; filename=""'.CRLF.$CTOS.CRLF.CRLF;
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="File3"; filename=""'.CRLF.$CTOS.CRLF.CRLF;
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="btnSubmit.x"'.CRLF.CRLF."45";
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="btnSubmit.y"'.CRLF.CRLF."7";
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="__EVENTTARGET"'.CRLF.CRLF."";
$body[] = $d.$boundary.CRLF.'Content-Disposition: form-data; name="__EVENTARGUMENT"'.CRLF.CRLF."";
$body[] = $d.$boundary."--";
$bodyString = implode(CRLF, $body);
$bodyLength = strlen($bodyString);

Please have a look at the code above.

Check the request. It looks like you’re adding duplicate new lines to the headers, the docs (http://www.php.net/manual/en/function.curl-setopt.php) imply you should not add them.

Can you please locate where is that ?

I think he is referring to your \r
characters in your $hdr declarations.

$hdr[] = "POST /abc/mypage.aspx HTTP/1.1\\r\
"; 
$hdr[] = "Host: mysite.com\\r\
"; 
$hdr[] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0\\r\
"; 
$hdr[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\\r\
"; 
$hdr[] = "Accept-Language: en-US,en;q=0.5\\r\
"; 
$hdr[] = "Accept-Encoding: gzip, deflate\\r\
"; 
$hdr[] = "Connection: keep-alive\\r\
"; 
$hdr[] = "Referer: https://www.mysite.com/abc/mypage.aspx\\r\
"; 
$hdr[] = "Cookie: ASP.NET_SessionId=$ASPSESS\\r\
"; 
$hdr[] = "Content-Type: multipart/form-data; boundry=".$d.$boundary."\\r\
";

Try removing ALL of the \r
in the above section.