如何使用超薄framwork获得POST请求的实体 [英] How to get the POST request entity using Slim framwork

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

问题描述

我已经在帖子实体像这样设置它发送采用了android的java JSON 数据:

  HttpPost httpPostRequest =新HttpPost(URLs.AddRecipe);
StringEntity本身=新StringEntity(jsonObject.toString());
httpPostRequest.setEntity(SE);

我怎样才能收到在 PHP这个 JSON 数据,其中我使用超薄框架
我曾尝试这样的:

  $ APP-GT&;后期('/配方/插入/,身份验证,()的函数使用($应用程序){
            $响应=阵列();
            $ JSON = $ APP-GT&;请求() - GT;后();
});


解决方案

JSON是不解析为 $ _ POST 超全局。在 $ _ POST 你可以找到表单数据。 JSON你可以请求主体,而不是找到。像下面的东西应该工作。

  $ APP-GT&;后(/配方/插入/,身份验证,()的函数使用($应用程序){
    $ JSON = $ APP-GT&;&请求 - GT; getBody();
    后续代码var_dump(json_de code($ 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);

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 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);
});

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

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