PHP Fetch all HTTP request headers

PHP Fetch all HTTP request headers

Today, We want to share with you PHP Fetch all HTTP request headers.
In this post we will show you Fetch all HTTP request headers, hear for we will give you demo and example for implement.
In this post, we will learn about Fetches all the headers sent by the server in response to a HTTP request with an example.

What is HTTP / Use of HTTP?

“HTTP is the protocol(rules) (the set of ‘rules’) for (client side to server side)transferring data (e.g. HTML in web pages,form data,document, pictures, files) between (both of side) web servers and client browsers data transfer, and usually should takes place on port 80 generally used.
This is a most where the ‘http://’ in website(server side) URLs comes from.”

PHP apache_request_headers() Function

“This function is used to all retrive information data in Apache as a module then the headers(data) the browser send(client side) can be retrieved using(server side) the apache_request_headers() function”

Example

Request
print_r( apache_request_headers() );
RESPONSE
Array
(
  [Host] => www.pakainfo.com //host name
  [Connection] => keep-alive
  [User-Agent] => Mozilla/10.0 (Windows; U; syWindows NT 6.1; en-US) Apple_WebKit/532.0 (KHTML, like Gecko) Chrome/5.0.209.1 Safari/632.0
  //get cache-control browser
  [Cache-Control] => max-age=1 //cach control
  [Accept] => application/xml,application/xhtml+xml+xhtml,text/html;q=0.5,text/plain;q=0.9,image/png,*/*;q=0.6
  [Accept-Encoding] => gzip,deflate,sdch //support
  [Accept-Language] => en-US,en;q=0.8 //defult language
  [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3 //set char
)

Fetch all HTTP request headers

Fetches all HTTP request headers from the current request.


	// all http headers store in this varible
    $headers_data = apache_request_headers();

	//get one by one header
    foreach ($headers_data as $header_info => $value_header) {
        echo "$header_info: $value_header 
\n";
    }

HTTP – get(RESPONSE) and set(REQUEST) custom HTTP headers using PHP ()

First of all let us start by sending(browser to server) a custom HTTP header(data) to the server. and then I will be using cURL(HTTP request) library to send HTTP requests(On server).

Sending The Request Header(Client side to server side )

first Let’s say our custom(static) header name is Example like (‘Authorization’) and we want to set(static value) its value to ‘123456’,
now create a file named send_http-request_data.php with the following content:

$uri_senddata = 'http://localhost/request_http.php';

$ch_data = curl_init($uri_senddata);

curl_setopt_array($ch_data, array(
    CURLOPT_HTTPHEADER  => array('Authorization: Jk9856895556Sd!@54d5ssfff'),
    CURLOPT_RETURNTRANSFER  =>true,//boolean true or false
    CURLOPT_VERBOSE     => 1
));
//output display
$out_data = curl_exec($ch_data);
//curl close
curl_close($ch_data);
// echo response output
echo $out_data;

Now we have sent(client to server) the request with method POST(secure).
The CURLOPT_VERBOSE option is used for debugging (debugging option 1,2,3.etc..)
when you want to output the (client side)request headers and (server side)the response headers.
and Now You all shall remove it on production.

Reading the custom header(Server side)

Now let’s make the reads that (make specific)custom header sent(client side data) from the curl request(and error and success message show).
and then create a file named http_request.php with the following contents and get all data.

print_r(apache_request_headers());

Custom Headers with PHP

To work around this we will have to (PHP run script)play around with the .htaccess but (set htaccess)you will need mod_rewrite.

[PHP]
setting htaccess file
RewriteEngine On
RewriteRule .? – [E=Authorization:%{HTTP:Authorization}]

and Now try to this in the http_request.php:

echo "Server message = ".$_SERVER['Authorization'];

We hope you get an idea about PHP Fetch all HTTP request headers
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Comment