如何在 PHP 中将 HTTP 请求中的所有信息打印到屏幕上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3136253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:49:15  来源:igfitidea点击:

How to print all information from an HTTP request to the screen, in PHP

phphttp

提问by Spike Williams

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.

我需要一些 PHP 代码来转储 HTTP 请求中的所有信息,包括头和包含在 POST 请求中的任何信息的内容。基本上,一个诊断工具可以准确地输出我发送到服务器的内容。

Does anyone have some code that does this?

有没有人有一些代码可以做到这一点?

采纳答案by Marco Ceppi

Lastly:

最后:

print_r($_REQUEST);

That covers most incoming items: PHP.net Manual: $_REQUEST

这涵盖了大多数传入项目:PHP.net 手册:$_REQUEST

回答by Vinko Vrsalovic

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.

需要进行一些按摩才能按照您想要的顺序获得所有内容,并排除您不感兴趣的变量,但应该给您一个开始。

回答by Peter Bailey

Well, you can read the entirety of the POST body like so

好吧,您可以像这样阅读整个 POST 正文

echo file_get_contents( 'php://input' );

And, assuming your webserver is Apache, you can read the request headers like so

而且,假设您的网络服务器是 Apache,您可以像这样读取请求标头

$requestHeaders = apache_request_headers();

回答by Cmyker

Nobody mentioned how to dump HTTP headers correctly under any circumstances.

没有人提到如何在任何情况下正确转储 HTTP 标头。

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.

来自 CGI 规范 rfc3875,第 4.1.18 节:

如果使用的协议是 HTTP,则名称以“HTTP_”开头的元变量包含从客户端请求标头字段读取的值。HTTP 标头字段名称转换为大写,所有出现的“-”都替换为“ ”,并在前面加上“HTTP”以给出元变量名称。

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

详情:http: //cmyker.blogspot.com/2012/10/how-to-dump-http-headers-with-php.html

回答by ldav1s

Putting together answers from Peter Baileyand Cmykeryou get something like:

Peter BaileyCmyker 的答案放在一起,您会得到如下结果:

<?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 -Sbuilt-in webserver, which is quite a handy feature of PHP.

它与php -S内置的网络服务器一起工作,这是 PHP 的一个非常方便的功能。

回答by matpie

If you want actual HTTP Headers (both request and response), give hurl.ita try.

如果您想要实际的 HTTP 标头(请求和响应),尝试hurl.it。

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.

您可以使用 PHP 命令apache_request_headers()获取请求标头和apache_response_headers()当前响应标头。请注意,只要内容尚未提供,就可以稍后在 PHP 脚本中更改响应。

回答by Roger Getnoticed

file_get_contents('php://input') will not always work.

file_get_contents('php://input') 并不总是有效。

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.

我在标题中有一个请求,其中“content-length=735”和“php://input”是空字符串。所以取决于 HTTP 请求的好坏程度。

回答by yassine j

The problem was, in the URL i wrote http://my_domaininstead of https://my_domain

问题是,在 URL 中我写了http://my_domain而不是https://my_domain

回答by Sergey Eremin

in addition, you can use get_headers(). it doesn't depend on apache..

此外,您可以使用get_headers()。它不依赖于 apache ..

print_r(get_headers());