How to get custom HTTP Headers PHP


This blog is about how to read All/Custom headers in PHP. Below code is used to get all Headers.

 $headers = getallheaders();

There is an alternative for getting HTTP headers.

apache_request_headers()

These functions are getting all HTTP headers like below:-
Array
(
    [Host] => localhost
    [Connection] => keep-alive
    [Content-Length] => 171
    [Authorization] => "My Custom Header Yo Yo!!"
    [Postman-Token] => 4c6308bd-da37-6fdf-6806-f945c59136f8
    [Cache-Control] => no-cache
    [Origin] => chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
    [User-Agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
    [Content-Type] => application/json
    [Accept] => */*
    [Accept-Encoding] => gzip, deflate, br
    [Accept-Language] => en-US,en;q=0.8
)


You can check output of headers Array. There is one header with key of  "Authorization". This is a custom header.

Below is function to get Header value by its key name. 

function get_header( $key )
{
     $headers = getallheaders();
    if ( array_key_exists($key, $headers) ) {
        $headerValue = $headers[ $key ];
    }
    return $headerValue;
}

Just call this function and pass key name of your header.

Happy Coding :D

Comments

Popular posts from this blog

How to change the color of Hamburger icon in Toolbar Android

How to combine two Bitmap in Android

Facebook Login Using LoginButton