0

This is my curl request (found it on some old stackoverflow answer):

$headers[]  = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
$headers[]  = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[]  = "Accept-Language:en-us,en;q=0.5";
$headers[]  = "Accept-Encoding:gzip,deflate";
$headers[]  = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[]  = "Keep-Alive:115";
$headers[]  = "Connection:keep-alive";
$headers[]  = "Cache-Control:max-age=0";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
    
return $data;

And I get this error:

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.

Do you have any ideea why I get this error and how should I fix it ?

Community
  • 1
  • 1
paulalexandru
  • 9,218
  • 7
  • 66
  • 94
  • I'm guessing you've seen this: http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning? – HPierce Aug 06 '16 at 01:09

2 Answers2

0

I think you should use CURLOPT_USERAGENT for User-Agent (my recommended way)

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0');

OR (not played with it)

$headers[]  = "User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";

instead of

$headers[]  = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

Another point to consider is whitespaces.

User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0

Wrong used

"...Agent : Mozilla.."

This will send you

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.

Community
  • 1
  • 1
mfkocak
  • 87
  • 4