Mark
Mark

Reputation: 5108

Get custom request header

Im running PHP version 5.5 on WAMP. I have a very simple API. I want to get the custom request header called "api_key". First of all, I made the GET request and logged the headers like this:

        foreach (getallheaders() as $name => $value) {
            $message .= "$name: $value\n";
        }

        file_put_contents('headers.log', $message); 

This resulted in:

Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript, */*; q=0.01
device_id: 63843
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
api_key: hv7Vgd4jsbb
Referer: http://localhost/server/cli/beaufort/www/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: PHPSESSID=bd3c8ce878ebc504b2128686efbe30cf;
bd3c8ce878ebc504b2128686efbe30cf=DEFAULT%7C0%7C2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55%2BUGpAo%3D%7C7456bf61db3500c8bb7b3bc38082a470ce4a2ad3

So "api_key" is there. However, somehow, when I do:

$message = $_SERVER['HTTP_API_KEY'];

I get the error:

Fatal error: Uncaught exception 'ErrorException' with message 'Undefined index: HTTP_API_KEY'

Why can I not get this header??

Upvotes: 2

Views: 7517

Answers (1)

Steve
Steve

Reputation: 20469

$headers = getallheaders();  
$message = $headers['api_key'];

Upvotes: 6

Related Questions