PHP: get the original HTTP request _ PHP Tutorial

Source: Internet
Author: User
PHP gets the original text of the HTTP request. 1. get the request line: Method, URI, and protocol can be obtained from the super variable $ _ SERVER. the values of the three variables are as follows: $ _ SERVER [REQUEST_METHOD] .. $ _ SERVER [REQUEST_URI] .. $ _ SERVER [1. request Line: Method, URI, protocol

You can obtain the value from the super variable $ _ SERVER. the values of the three variables are as follows:

$ _ SERVER ['request _ method']. ''. $ _ SERVER ['request _ URI ']. ''. $ _ SERVER ['server _ protocol']. "\ r \ n ";
2. retrieve all headers

PHP has a built-in function getallheader (), which is an alias of the apache_request_headers () function. all headers of the HTTP request can be returned in an array. However, this function can only work in Apache. if Nginx or command line is changed, an error that does not exist will be reported directly.

The common method is to extract from the super variable $ _ SERVER. the key values of headers start with "HTTP _". you can obtain all headers based on this feature. The code is as follows:

Function get_all_headers (){
$ Headers = array ();

Foreach ($ _ SERVER as $ key => $ value ){
If (substr ($ key, 0, 5) = 'http _'){
$ Key = substr ($ key, 5 );
$ Key = strtolower ($ key );
$ Key = str_replace ('_', '', $ key );
$ Key = ucwords ($ key );
$ Key = str_replace ('', '-', $ key );

$ Headers [$ key] = $ value;
}
}

Return $ headers;
}
3. get the Body

The official website provides a method to obtain the request Body, namely:

File_get_contents ('php: // input ')
4. final code

/**
* Get the original HTTP request
* @ Return string
*/
Function get_http_raw (){
$ Raw = '';

// (1) request line
$ Raw. = $ _ SERVER ['request _ method']. ''. $ _ SERVER ['request _ URI ']. ''. $ _ SERVER ['server _ protocol']. "\ r \ n ";

// (2) request Headers
Foreach ($ _ SERVER as $ key => $ value ){
If (substr ($ key, 0, 5) = 'http _'){
$ Key = substr ($ key, 5 );
$ Key = str_replace ('_', '-', $ key );

$ Raw. = $ key. ':'. $ value. "\ r \ n ";
}
}

// (3) empty rows
$ Raw. = "\ r \ n ";

// (4) request Body
$ Raw. = file_get_contents ('php: // input ');

Return $ raw;
}

Http://www.bkjia.com/PHPjc/364740.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364740.htmlTechArticle1. get the REQUEST line: Method, URI, and protocol can be obtained from the super variable $ _ SERVER. the values of the three variables are as follows: $ _ SERVER ['request _ method']. ''. $ _ SERVER ['request _ URI ']. ''. $ _ SERVER ['...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.