17

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;
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • possible duplicate of [How to get the POST request entity using Slim framwork](http://stackoverflow.com/questions/26346960/how-to-get-the-post-request-entity-using-slim-framwork) – guillermoandrae Jan 21 '15 at 18:09

3 Answers3

43

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
});
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
guillermoandrae
  • 600
  • 5
  • 10
  • I tried with $application->request->params('paramName'); as well as $allPostVars = $application->request->post('paramName); but still I didn't get any POST request variable. Only after $body = $application->request->getBody(); I'm getting the POST request data. –  Jan 21 '15 at 17:51
  • 2
    Ah I understand now. Then this is a duplicate of this: http://stackoverflow.com/questions/26346960/how-to-get-the-post-request-entity-using-slim-framwork – guillermoandrae Jan 21 '15 at 17:59
  • 2
    To expound: you'll just need to use `json_decode` on the request body to parse the response and get to the information. – guillermoandrae Jan 21 '15 at 18:01
  • Thanks for your help. But as you understood my exact issue can you please post a more meaningful and descriptive answer along with corrected and modified version of my code it would be of great help to me as well as other people from PHP community? –  Jan 21 '15 at 18:04
  • 2
    JSON isn't parsed into $_POST -- it is sent as the contents of the request body -- so it can't be accessed using the `post()` and `params()` methods. So to get to the data, you'll have to do something like: `json_encode($application->request->getBody(), true)` to parse the JSON string into an associative array and use as you wish. – guillermoandrae Jan 21 '15 at 18:07
  • :Thanks once again for providing this much useful information. Why don't you replace your answer and write a new one with new code. I'll immediately accept it. –  Jan 21 '15 at 18:20
15

"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'];
lurker123456
  • 171
  • 1
  • 2
0

Slim will json_decode the post body for you using this middleware

https://www.slimframework.com/docs/v4/middleware/body-parsing.html

Joel
  • 814
  • 2
  • 9
  • 19