且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP ::获取POST请求内容

更新时间:2023-02-23 20:01:57

使用 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");