如何在 PHP 中访问 POST 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/664148/
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-24 23:24:59  来源:igfitidea点击:

How to access POST data in PHP?

phphttppost

提问by phihag

I need to write a PHP page which would accept an XML document sent over a POST request like this:

我需要编写一个 PHP 页面,该页面将接受通过 POST 请求发送的 XML 文档,如下所示:

POST /mypage.php HTTP/1.1
Host: myhost.com
Content-Type: application/xml
Content-Length: ...

<?xml version="1.0" encoding="utf-8"?>
<data>
 ...
</data>

This is not data from some HTML form, just a plain XML document.

这不是来自某些 HTML 表单的数据,只是一个普通的 XML 文档。

How can I access this XML in my PHP code?

如何在我的 PHP 代码中访问这个 XML?

回答by phihag

Read from php://input. For example, you could use:

php://input. 例如,您可以使用:

$rawdata = file_get_contents('php://input');

or

或者

$rootNode = simplexml_load_file('php://input');

The alternative, using $HTTP_RAW_POST_DATA, works, too - but it's slower and needs the PHP configuration always_populate_raw_post_data.

替代方法 using 也$HTTP_RAW_POST_DATA可以工作 - 但速度较慢并且需要 PHP 配置always_populate_raw_post_data

回答by Bob Gettys

http://us.php.net/manual/en/reserved.variables.httprawpostdata.php

http://us.php.net/manual/en/reserved.variables.httprawpostdata.php

$HTTP_RAW_POST_DATA should be available assuming the content-type of the request was not multipart/form-data

假设请求的内容类型不是 multipart/form-data,$HTTP_RAW_POST_DATA 应该可用

回答by Rob Di Marco

You probably want to use the PHP input. Something like

您可能想要使用PHP 输入。就像是

$postText = trim(file_get_contents('php://input'));

回答by fidazik

Any of these would work:

其中任何一个都可以:

$HTTP_RAW_POST_DATA variable or the php://input stream.

However, for the

然而,对于

$HTTP_RAW_POST_DATA variable

to work, you might need to change .ini setting in your configuration file

要工作,您可能需要更改配置文件中的 .ini 设置

回答by Alekc

If you use print_r($_POST); you will be able to see what you got. Unless i'm missing something... Edit: nvm, totaly forgot about Raw Data :/

如果您使用 print_r($_POST); 你将能够看到你得到了什么。除非我遗漏了什么......编辑:nvm,完全忘记了原始数据:/