How to print all information from an HTTP request to the screen, in PHP
PhpHttpPhp Problem Overview
I need some PHP code that does a dump of all the information in an HTTP request, including headers and the contents of any information included in a POST request. Basically, a diagnostic tool that spits out exactly what I send to a server.
Does anyone have some code that does this?
Php Solutions
Solution 1 - Php
Lastly:
print_r($_REQUEST);
That covers most incoming items: [PHP.net Manual: $_REQUEST][1]
[1]: http://us2.php.net/manual/en/reserved.variables.request.php "PHP.net Manual: $_REQUEST"
Solution 2 - Php
A simple way would be:
<?php
print_r($_SERVER);
print_r($_POST);
print_r($_GET);
print_r($_FILES);
?>
A bit of massaging would be required to get everything in the order you want, and to exclude the variables you are not interested in, but should give you a start.
Solution 3 - Php
Well, you can read the entirety of the POST body like so
echo file_get_contents( 'php://input' );
And, assuming your webserver is Apache, you can read the request headers like so
$requestHeaders = apache_request_headers();
Solution 4 - Php
Nobody mentioned how to dump HTTP headers correctly under any circumstances.
> From CGI specification rfc3875, section 4.1.18: > > Meta-variables with names beginning with "HTTP_" contain values read > from the client request header fields, if the protocol used is HTTP. > The HTTP header field name is converted to upper case, has all > occurrences of "-" replaced with "" and has "HTTP" prepended to give > the meta-variable name.
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$chunks = explode('_', $key);
$header = '';
for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
$header .= ucfirst(strtolower($chunks[$i])).'-';
}
$header .= ucfirst(strtolower($chunks[$i])).': '.$value;
echo $header.'<br>';
}
}
Details: http://cmyker.blogspot.com/2012/10/how-to-dump-http-headers-with-php.html
Solution 5 - Php
Putting together answers from Peter Bailey and Cmyker you get something like:
<?php
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$chunks = explode('_', $key);
$header = '';
for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
$header .= ucfirst(strtolower($chunks[$i])).'-';
}
$header .= ucfirst(strtolower($chunks[$i])).': '.$value;
echo $header."\n";
}
}
$body = file_get_contents('php://input');
if ($body != '') {
print("\n$body\n\n");
}
?>
which works with the php -S
built-in webserver, which is quite a handy feature of PHP.
Solution 6 - Php
If you want actual HTTP Headers (both request and response), give hurl.it a try.
You can use the PHP command apache_request_headers()
to get the request headers and apache_response_headers()
to get the current response headers. Note that response can be changed later in the PHP script as long as content has not been served.
Solution 7 - Php
file_get_contents('php://input') will not always work.
I have a request with in the headers "content-length=735" and "php://input" is empty string. So depends on how good/valid the HTTP request is.
Solution 8 - Php
in addition, you can use get_headers(). it doesn't depend on apache..
print_r(get_headers());
Solution 9 - Php
The problem was, in the URL i wrote http://my_domain instead of https://my_domain