I’m trying to login to a site via PHP cURL and I’m only getting “Bad Request” responses.
I played around with hosts file and set it to my server to check which Request Headers my browser sends and compare it to the request headers sent by cURL.
Everything is equal, except of:
Browser:
Content-Type: application/x-www-form-urlencoded
Content-Length: 51
PHP cURL:
Content-Length: 51, 359
Content-Type: application/x-www-form-urlencoded; boundary=----------------------------5a377b7e6ba7
I already set that values with this command, but it still sends the wrong headers:
curl_setopt($this->hCurl, CURLOPT_HTTPHEADER, array(
'Expect:',
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: 51'
));
You shouldn’t have to set the content-length yourself. If you use cURL to send an HTTP POST, it will calculate the content length for you.
If you set the
CURLOPT_POSTFIELDSvalue as an array, it will automatically submit the request asmultipart/form-dataand use a boundary. If you pass a string, it will useapplication/x-www-form-urlencodedso make sure you pass a urlencoded string toCURLOPT_POSTFIELDSand not an array since you want form-urlencoded.You need to be doing this:
In either case, you do not need to set the content length, but you have to use the first method to get
application/x-www-form-urlencodedencoding on the form.If that doesn’t help, post all the code relevant to setting up the curl request, (all the options, and data you are passing to it) and that should help solve the problem.
EDIT:
Added is an example I came up with that works (I get failed login).