How to get the POST request entity using Slim framework

风格不统一 提交于 2020-01-02 19:13:10

问题


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

回答1:


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



回答2:


You need to get the response body. Save it in a variable. After that, verify if the variable is null and then decode your JSON.

$app->post("/recipe/insert/", "authenticate", function() use ($app) { 
$entity = $app->request->getBody(); 

if(!$entity)
   $app->stop();

$entity = json_decode($entity, true);

});


来源:https://stackoverflow.com/questions/26346960/how-to-get-the-post-request-entity-using-slim-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!