After execution of this step I'm getting the above weird format data as a response on iPhone. Header("Content-Type: application/json") //setting header before sending the JSON response back to the iPhoneĮcho json_encode($new_body) // Converting the request body into JSON format and sending it as a response back to the iPhone. 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?įollowing is the necessary code snippet: request->getBody() Encoding JSON Data in PHP In PHP the jsonencode () function is used to encode a value to JSON format. Both functions only works with UTF-8 encoded string data. ![]() These functions are jsonencode () and jsondecode (), respectively. Though they are not accessible in $application->request->post(), they are coming into request body. PHP has built-in functions to encode and decode JSON data. ![]() Then I got the parameters' values but they are in very weird format as follows: one thing for sure is POST request parameters are coming to this API file. 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. $allPostVars = $application->request->post() //Always I get null Reconsider this later to follow next jsondecode implemention from core. ![]() But again, I understand the point that slim should not do too much. To resolve this, ensure the response or the file used in the program is not empty or conditionally check for the content type before parsing. When I tried to print the POST parameters' values I got "null" for every parameter. In php 7.3 json decode can throw exception rather than failing silently, failing silently is not a good practice and php core fixed it. This json decoder error occurs in Python because the user tries to parse something that is not valid JSON. Now let us see simple example which uses jsondecode () of PHP in a HTML structure. This POST request is in JSON format.īut I'm not able to access the POST parameters that are sent in a request from iPhone. The jsondecode() function is used to decode or convert a JSON object to a PHP object. The body option is used to control the body of an entity enclosing request (e.g., PUT, POST, PATCH). This jsondecode () function of PHP takes JSON encoded string as input and returns the value that is decoded to PHP objects which of PHP data types and if the decoding is not possible then this function returns null instead of PHP objects. I've written one API using Slim framework.Ī POST request is coming to this API from an iPhone app.
0 Comments
Leave a Reply. |