如何读取 PHP 中的 GET 和 POST 请求(post 在 json 中)

How to read GET and POST request in PHP (post is in json)

我有一个 javascript(不是我的)来同步客户端 (javascript) 和服务器 (php) 之间的信息和 javascript 发送到服务器 GET 和POST 信息,我可以读取简单的 GET 信息,所以我尝试读取 $_POST 信息,但无法检索 POST 信息。

我尝试 print_r($_POST) 然后 returns Array() 什么都没有。

我尝试在 Firefox 中使用 Firebug 和控制台查看它,我看到了

如何检索 json 字符串并将其处理为 >Transmission de la requete< 我的 PHP 代码?我可以轻松检索 GET 参数,例如 token、src 等,因此我无法检索 POST 传输。

感谢您的帮助!

我真的不明白你的意思。但是因为它像 JSON 请求发送到页面。您可以使用 file_get_contents('php://input') 获取发送的任何 JSON 请求。然后你可以把它解码成一个对象或数组。

示例

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

获取对象

$input = json_decode($request);

所以输入字段将是

$input->name; $input->password;

获取数组

$input = json_decode($request, true);

所以输入字段将是

$input[name]; $input[password];