PHP Yii下,$_REQUEST获取不到前端post参数,改为用php://input 或 修改配置

探讨了前端使用Umi框架提交POST表单数据至PHP端的问题,详细介绍了如何通过调整Content-Type及使用$file_get_contents('php://input')正确接收JSON格式参数的方法。

前端使用的Umi框架,提交post表单数据,在php端我用$_REQUEST去接受数据,是个空。并没有接受到参数。

用postman发送同样的请求,$_REQUEST就可以接收到参数数据。

开始只知道传过来的参数是json格式,以为Content-Type:application/x-www-data-urlencoded是这种形式的。后来我改为用

  $body = @file_get_contents('php://input');//接受整个请求主体
  $body=json_decode($body)  ;//反序列化
  $id=$body->id;//获取欲取参数
  echo $id;

这样获取数据就可以获取到。

如果想使用数组:$body=json_decode($body,true) ;

但又发现使用postman请求获取不到数据了,将postman的发送时方式改为:raw:text之后就都可以正常接收数据了。

参考原文:https://www.cnblogs.com/evaling/p/7607603.html

 

php://input, $_POST与$_GET获取请求输入的差别

原文地址:https://www.cnblogs.com/ningskyer/articles/4712597.html

小结:

  1. Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST
  2. PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA
  3. 只有Coentent-Type不为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。
  4. 只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。
  5. php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini
  6. PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。

补充

前端传参格式为 content-type: application/json;charset=UTF-8时。YII用yii::$app->request->post()获取不到参数。加入以下配置即可

 request中加入

'parsers' => [

              'application/json' => 'yii\web\JsonParser',

              'text/json' => 'yii\web\JsonParser',

 ]

 

 

public function actionConfirmRefund() { if (empty($_POST)) { $raw = file_get_contents('php://input'); $json = json_decode($raw, true); if (is_array($json)) { foreach ($json as $k => $v) { $_POST[$k] = $v; $_REQUEST[$k] = $v; } } } $order = $this->getParam('order', []); $refund_type = trim($this->getParam('refund_type', '')); $reconciliation_date = $this->getParam('reconciliation_date', ''); $requiredFields = ['game_id', 'app_id', 'orderno', 'amount', 'createdtime']; foreach ($requiredFields as $field) { if (!isset($order[$field]) || $order[$field] === '') { $this->renderJson([-1, "订单信息缺少必填字段: $field"], []); } } if (!is_numeric($order['amount']) || $order['amount'] < 0) { $this->renderJson([-1, "订单金额格式不正确"], []); } if (empty($order['createdtime']) || strtotime($order['createdtime']) === false) { $this->renderJson([-1, '订单创建时间格式不正确'], []); } if (!in_array($refund_type, ['refund', 'selfDeduction'])) { $this->renderJson([-1, '退费类型无效'], []); } $exists = RefundOrder::model()->findByAttributes(['order_no' => $order['orderno']]); if ($exists) { $this->renderJson([-1, '该订单已确认退费'], []); } $adminName = Yii::app()->user->getName(); RefundOrder::model()->insertRefundOrder($order, $adminName); $result = RefundOrder::model()->confirmRefund($order['orderno'], $refund_type, $reconciliation_date); $this->renderJson([$result['code'], $result['message']], $response); } 帮我检查当前代码为什么会报错{ "code": -1, "message": "订单信息缺少必填字段: game_id", "data": {} },如何解决
最新发布
06-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值