How to access a JSON request body of a POST request in Slim?

狂风中的少年 提交于 2019-12-18 19:08:09

问题


I'm just a newbie in the Slim framework. I've written one API using Slim framework.

A POST request is coming to this API from an iPhone app. This POST request is in JSON format.

But I'm not able to access the POST parameters that are sent in a request from iPhone. When I tried to print the POST parameters' values I got "null" for every parameter.

$allPostVars = $application->request->post(); //Always I get null

Then I tried to get the body of a coming request, convert the body into JSON format and sent it back as a response to the iPhone. Then I got the parameters' values but they are in very weird format as follows:

"{\"password\":\"admin123\",\"login\":\"admin@gmail.com\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"

So one thing for sure is POST request parameters are coming to this API file. Though they are not accessible in $application->request->post(), they are coming into request body.

My first issue is how should I access these POST parameters from request body and my second issue is why the request data is getting displayed into such a weird format as above after converting the request body into JSON format?

Following is the necessary code snippet:

<?php

    require 'Slim/Slim.php';    

    \Slim\Slim::registerAutoloader();

    //Instantiate Slim class in order to get a reference for the object.
    $application = new \Slim\Slim();

    $body = $application->request->getBody();
    header("Content-Type: application/json");//setting header before sending the JSON response back to the iPhone
    echo json_encode($new_body);// Converting the request body into JSON format and sending it as a response back to the iPhone. After execution of this step I'm getting the above weird format data as a response on iPhone.
    die;
?>

回答1:


Generally speaking, you can access the POST parameters individually in one of two ways:

$paramValue = $application->request->params('paramName');

or

$paramValue = $application->request->post('paramName');

More info is available in the documentation: http://docs.slimframework.com/#Request-Variables

When JSON is sent in a POST, you have to access the information from the request body, for example:

$app->post('/some/path', function () use ($app) {
    $json = $app->request->getBody();
    $data = json_decode($json, true); // parse the JSON into an assoc. array
    // do other tasks
});



回答2:


"Slim can parse JSON, XML, and URL-encoded data out of the box" - http://www.slimframework.com/docs/objects/request.html under "The Request Body".

Easiest way to handle a request in any body form is via the "getParsedBody()". This will do guillermoandrae example but on 1 line instead of 2.

Example:

$allPostVars = $application->request->getParsedBody();

Then you can access any parameters by their key in the array given.

$someVariable = $allPostVars['someVariable'];


来源:https://stackoverflow.com/questions/28073480/how-to-access-a-json-request-body-of-a-post-request-in-slim

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