Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't curl send my headers in PHP?

The following code:

$ch = curl_init('http://localhost/testweb/search.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Encoding    gzip, deflate',
        'Accept-Language    en-US,en;q=0.5',
        'Connection keep-alive',
        'SomeBull   BeingIgnored',
        'Cookie CLASSICPAGE=off',
        'User-Agent Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
        ));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$body = substr($response, -$info['download_content_length']);
echo $body;

has the following output (php.exe mycurl.php):

Host: localhost
Accept: */*
User-Agent      Mozilla/5.0 (Windows NT 5.1; rv: 16.0) Gecko/20100101 Firefox/16.0

The search.php on localhost:

error_reporting(0);
header("Content-Type: text/plain");
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

My question is: what happened to the headers I set?

like image 628
HMR Avatar asked Oct 21 '12 04:10

HMR


1 Answers

Headers are in the format:

Header: value

Your example is missing the colon on each of the headers. Just adjust it like so:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Connection: keep-alive',
    'SomeBull: BeingIgnored',
    'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
  )
);
like image 155
Owen Avatar answered Oct 01 '22 14:10

Owen