如何使用Slim框架获取POST请求实体 [英] How to get the POST request entity using Slim framework

查看:169
本文介绍了如何使用Slim框架获取POST请求实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过在POST实体中设置这样的数据,使用Android Java发送了JSON数据:

I have sent JSON data using android java by setting it in the post entity like this:

HttpPost httpPostRequest = new HttpPost(URLs.AddRecipe);
StringEntity se = new StringEntity(jsonObject.toString());
httpPostRequest.setEntity(se);

如何在使用Slim frameworkphp中接收此json数据? 我已经尝试过:

How can I receive this json data in the php , where I am using Slim framework ? I have tried this:

$app->post('/recipe/insert/', 'authenticate', function() use ($app) { 
            $response = array();
            $json = $app->request()->post(); 
});

推荐答案

JSON未解析为$_POST超全局变量.在$_POST中,您可以找到表单数据.您可以在请求正文中找到JSON.像下面这样的东西应该起作用.

JSON is not parsed into $_POST superglobal. In $_POST you can find form data. JSON you can find in request body instead. Something like following should work.

$app->post("/recipe/insert/", "authenticate", function() use ($app) { 
    $json = $app->request->getBody(); 
    var_dump(json_decode($json, true));
});

这篇关于如何使用Slim框架获取POST请求实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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