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 ['...