如何检索请求负载 [英] How to retrieve Request Payload

查看:89
本文介绍了如何检索请求负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP ExtJS ajax商店



它不在POST或GET中发送数据(在创建,更新,销毁)。在 Chrome控制台中,我在请求有效载荷字段中看到我的传出参数为JSON。 $ _ POST $ _ GET 是空的。



如何使用PHP检索?

解决方案

如果我正确地了解情况,你只是通过http身份传递json数据,而不是 application / x-www您可以使用此代码段获取这些数据:



pre> $ request_body = file_get_contents('php:// input');

如果你通过json,那么你可以这样做:

  $ data = json_decode($ request_body); 

$ data 然后包含json数据php数组。



php://输入是一个所谓的 wrapper


php://输入是一个读 - 只允许您从请求体读取原始数据
。在POST请求的情况下,最好使用
使用php://输入而不是$ HTTP_RAW_POST_DATA,因为它不是
依赖于特殊的php.ini指令。此外,对于
$ HTTP_RAW_POST_DATA未默认填充的情况,对于激活
always_populate_raw_post_data,这是一个潜在的
更少的内存密集型替代方案。 php://输入不可用
enctype =multipart / form-data。



I'm using PHP, ExtJS and ajax store.

It sends data (on create, update, destroy) not in POST or GET. In the Chrome Console I see my outgoing params as JSON in the "Request Payload" field. $_POST and $_GET are empty.

How to retrieve it in PHP?

解决方案

If I understand the situation correctly, you are just passing json data through the http body, instead of application/x-www-form-urlencoded data.

You can fetch this data with this snippet:

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

If you are passing json, then you can do:

$data = json_decode($request_body);

$data then contains the json data is php array.

php://input is a so called wrapper.

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

这篇关于如何检索请求负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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