I'm looking for a way to get, using native PHP, the raw HTTP request that my script received, including headers and body. Reading the PHP documentation I cant found a standard way to get the raw request, regardless the HTTP method used.
For example, when the page test.php is requested, I want to get the complete request like:
GET /test.php HTTP/1.1
Host:....
....
....
The same in case of POST, HEAD, etc...
Seems very strange that there doesn't exist a method to access the raw request buffer!
The raw request is not available to PHP, because the request has already been consumed by the time PHP starts.
When PHP is running as an Apache module (
mod_php
), for instance, the request is received and parsed by Apache, and PHP is only invoked after Apache has parsed that request and determined that it refers to a file which should be processed by PHP. If PHP is running as a CGI or FastCGI handler, it never receives an HTTP request at all — it only sees the CGI form of the request, which is quite different.Looking over the manual there doesn't seem to be an unparsed raw access to the request to match what you want, so I suspect you will need to re-build what you want from the
$_SERVER
variables. A quick search I found this class, made some small change to get theGET / HTTP/1.1
, perhaps you will find it suits your needs.P.S: Dont forget to htmlentities() them values on output :)
If you are on an Apache machine try this:
http://php.net/manual/en/function.getallheaders.php