Sadikhasan
Sadikhasan

Reputation: 18600

Error 400 Bad request when I use PHP CURL

When I CURL in PHP. With appropriate headers and fields but It will give me error Error 400 Bad request. Why this error becomes?

HTTP/1.0 400 Bad request Cache-Control: no-cache Connection: close Content-Type: text/html
400 Bad request
Your browser sent an invalid request. 1

Header Request

$header[] = "Accept:application/json, text/javascript, */*; q=0.01";
$header[]="Accept-Encoding:gzip, deflate";
$header[] = "Cache-Control:max-age=0";
$header[]= "Accept-Language:en-US,en;q=0.5";
$header[] = "Content-Length:37";
$header[]="Content-Type:application/x-www-form-urlencoded; charset=UTF-8";
$header[]="Cookie:__qca=P0-116849880-1387336057175; __utma=140029553.335591273.1387336057.1389609300.1389617402.102; __utmz=140029553.1389617402.102.89.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); _ga=GA1.2.335591273.1387336057; sgt=id=3380ce36-a139-4845-bd20-5bb3fd4174ec; usr=t=qrthm51g2UyV&s=QtuIYj84zEOR";
$header[]="X-Requested-With:XMLHttpRequest";

CURL Code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_POST, $fields);
curl_setopt($ch, CURLOPT_, $value);
$response=curl_exec($ch);
curl_close($ch);

If you want any further data please comment I will explain in detail.

Upvotes: 2

Views: 33338

Answers (1)

Stephen C
Stephen C

Reputation: 719416

A 400 response means that the HTTP server that the request went to has said that the request is invalid.

Why?

Who knows! Maybe the URL you sent in the request has malformed arguments (in the query string) or maybe it requires arguments that you didn't supply. Or maybe the problem is the other headers.

Or maybe it just doesn't like you.

I suggest that you compare the request you are sending with either the web site's API documentation or a request sent by a (real) browser. And if that fails, ask the folks who look after the web server you are sending the requests to.

Upvotes: 4

Related Questions