如何使用 PHP/apache 访问原始 HTTP 请求数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165603/
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
How can I access the raw HTTP request data with PHP/apache?
提问by Shabbyrobe
I was wondering if there was a way to get at the raw HTTP request data in PHP running on apache that doesn't involve using any additional extensions. I've seen the HTTPfunctions in the manual, but I don't have the option of installing an extension in my environment.
我想知道是否有一种方法可以在不涉及使用任何其他扩展的情况下获取在 apache 上运行的 PHP 中的原始 HTTP 请求数据。我已经在手册中看到了HTTP函数,但是我没有在我的环境中安装扩展的选项。
While I can access the information from $_SERVER, I would like to see the raw request exactly as it was sent to the server. PHP munges the header names to suit its own array key style, for eg. Some-Test-Header becomes HTTP_X_SOME_TEST_HEADER. This is not what I need.
虽然我可以从 $_SERVER 访问信息,但我希望看到原始请求与发送到服务器时完全一样。PHP 修改标题名称以适应其自己的数组键样式,例如。Some-Test-Header 变为 HTTP_X_SOME_TEST_HEADER。这不是我需要的。
采纳答案by Owen
Do you mean the information contained in $_SERVER?
你的意思是包含在 中的信息$_SERVER?
print_r($_SERVER);
Edit:
编辑:
Would this do then?
这样做可以吗?
foreach(getallheaders() as $key=>$value) {
print $key.': '.$value."<br />";
}
回答by Bretticus
Use the following php wrapper:
使用以下 php 包装器:
$raw_post = file_get_contents("php://input");
回答by tim
Try this:
尝试这个:
$request = $_SERVER['SERVER_PROTOCOL'] .' '. $_SERVER['REQUEST_METHOD'] .' '. $_SERVER['REQUEST_URI'] . PHP_EOL;
foreach (getallheaders() as $key => $value) {
$request .= trim($key) .': '. trim($value) . PHP_EOL;
}
$request .= PHP_EOL . file_get_contents('php://input');
echo $request;

