【问题标题】:How to get the POST request entity using Slim framework如何使用 Slim 框架获取 POST 请求实体
【发布时间】:2014-12-08 10:18:24
【问题描述】:

我已经使用 android java 通过在 post 实体中设置它来发送JSON 数据,如下所示:

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

如何在我使用 Slim frameworkphp 中接收此 json 数据? 我试过这个:

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

【问题讨论】:

    标签: php android json httprequest slim


    【解决方案1】:

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

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

    【讨论】:

    • 修复你的 var_dump 函数。它缺少右括号
    【解决方案2】:

    您需要获取响应正文。将其保存在变量中。之后,验证变量是否为空,然后解码您的 JSON。

    $app->post("/recipe/insert/", "authenticate", function() use ($app) { 
    $entity = $app->request->getBody(); 
    
    if(!$entity)
       $app->stop();
    
    $entity = json_decode($entity, true);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2015-03-13
      • 2015-08-19
      • 1970-01-01
      • 2019-11-09
      • 2013-06-12
      相关资源
      最近更新 更多