简体   繁体   English

从HTTP请求主体解析POST请求

[英]parse POST request from HTTP Request body

What is the best and easiest way to get the HTTP Body from the POST request and parse it with variables? 从POST请求中获取HTTP正文并使用变量进行解析的最佳和最简便的方法是什么? Say I have two attributes called latitude and longitude in the body in which the values I want to extract. 假设我要在其中提取值的主体中有两个属性,分别称为纬度和经度。

Another thing is how can I test it so I can see these values? 另一件事是如何测试它才能看到这些值?

In your PHP which handles the POST request you can do something like: 在处理POST请求的PHP中,您可以执行以下操作:

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];

Some browsers have developer tools that allow you to see the POST data that the browser sent. 某些浏览器具有开发人员工具,使您可以查看浏览器发送的POST数据。 I use the FireBug plug-in for Firefox. 我使用Firefox的FireBug插件。 I think the built-in developer tools in IE 8 and above (F12 I think) can do the same thing. 我认为IE 8及更高版本(我认为F12)中的内置开发人员工具可以完成相同的操作。 Not sure about Chrome, but I'm sure there's something around. 不确定Chrome,但我确定周围有问题。

You're a little unclear in your question, if what you're asking is how to get the values of post variables, then PHP has the $_POST super global that you can reference as $_POST['key']. 您的问题有点不清楚,如果您要问的是如何获取post变量的值,那么PHP具有$ _POST超全局变量,您可以将其称为$ _POST ['key']。

If what you're asking is how to get some xml or something from the post body and parse it for values you might try the $HTTP_RAW_POST_DATA super global. 如果您要问的是如何从帖子正文中获取一些xml或某些内容,然后将其解析为值,则可以尝试使用$ HTTP_RAW_POST_DATA超级全局变量。 It would depend how the data is formatted when it gets there. 这将取决于数据到达那里后的格式。

read to $_POST like this: 像这样读到$ _POST:

echo '<pre>';
  print_r ( $_POST );
echo '</pre>'

and to access: $_POST [variablename] read more at: http://us2.php.net/manual/en/reserved.variables.post.php 并访问: $_POST [variablename]请访问: http//us2.php.net/manual/en/reserved.variables.post.php了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM