PHP ::获取POST请求内容 [英] PHP:: Get POST Request Content

查看:125
本文介绍了PHP ::获取POST请求内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebClient.UploadData()方法(C#)向我的网络服务器发送POST请求。发送到我的网络服务器的数据包如下所示:

I'm sending a POST request using WebClient.UploadData() method (C#) to my webserver. The packet sent to my webserver looks like so:

POST / HTTP/1.1
Host: {ip}
Content-Length: {length}
Expect: 100-continue
Connection: Keep-Alive


{buffer_content}

由于$ _POST数组中没有{buffer_content}分配,我有以下问题......

As the {buffer_content} is nowhere assigned in the $_POST array, I have the following question...

问题:如何用PHP阅读{buffer_content}?

Question: How do I read the {buffer_content} with PHP?

我偶然发现 file_get_contents('php:// input'),但我不确定是否建议这样做。

I've stumbled upon file_get_contents('php://input'), but I'm unsure whether that is recommended to do.

推荐答案

使用 php://输入流:

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

这是推荐的方法,在PHP 7.0中,这是唯一的方法。以前,有时会有一个名为 $ HTTP_RAW_POST_DATA 的全局变量,但它是否存在将取决于INI设置,并且创建它会损害性能。该变量已被弃用并删除。

This is the recommended way to do this and, in PHP 7.0, the only way. Previously, there was sometimes a global variable called $HTTP_RAW_POST_DATA, but whether it existed would depend on an INI setting, and creating it hurt performance. That variable was deprecated and removed.

请注意,在PHP 5.6之前,您只能读取 php:// input 一次,所以请确保存储它。

Beware that prior to PHP 5.6, you can only read php://input once, so make sure you store it.

一旦你拥有了你的身体,你就可以从JSON或其他任何东西解码它,如果你需要的话:

Once you have your body, you can then decode it from JSON or whatever, if you need that:

$requestBody = json_decode($requestBody) or die("Could not decode JSON");

这篇关于PHP ::获取POST请求内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆