Getting JSON from network request body in PHP

TL;DR version:
How do I pull a JSON object from the body of a network.request body? $_POST(‘JSONvariableName’) doesn’t exactly work(not that i expected it too!)

Long version with more explanation:
So, this is kind of a hybrid PHP/Corona question. What I am trying to do is use the network.request to POST a table that has been encoded into a JSON object that i store in the body of the request. The problem is… I don’t know how to pull the object out of the body on the PHP side. I know how to read query strings that I explicitly name, such as “score=100&level=2” or whatever but not really a JSON object whose only reference was it’s variable name in Lua. I have a feeling it’s not a difficult thing to do but my googling/forum searching skills and my non-educated guesses have not proved fruitful.

Here is some striped down sample code. I really don’t have any PHP code to provide since I am semi-clueless on what to do anyway. Actually the Lua code is probably useless given my question but here it is anyway.
[lua]-- define header
local headers = {}
headers[“Content-Type”] = “application/json”
headers[“Accept-Language”] = “en-US”

–define body
local myBody = { level=1, gametype = 5, speed = 3, score = 100 };
local jsonBody = json.encode(myBody)

–add header/body to myParams
local myParams = {}
myParams.headers = headers
myParams.body = myBody

–send request
network.request(“http://mywebsite.com/webpage.php","POST”, myNetworkListener, myParams)[/lua]
[import]uid: 147305 topic_id: 28319 reply_id: 328319[/import]

I’ve actually decided to use a different method entirely to achieve what I needed to do but if anyone wants to chime in on the original question I would still be interested in the answer! [import]uid: 147305 topic_id: 28319 reply_id: 114453[/import]

something like this:

$myBody = Array();$myBody = json_decode($_POST["myBody"]);print_r($myBody);?>[/code] [import]uid: 19626 topic_id: 28319 reply_id: 114469[/import]

I had a feeling you would respond, robmiracle, seeing as you were in almost every php thread i found on these forums!

I think that is what I had but for some reason the $_post(“myBody”) didn’t return anything. If you can actually call it that way, using what you named the variable in Lua, then I probably had an issue elsewhere. I just started playing with PHP Monday so it could of been anything. Thanks for the reply! [import]uid: 147305 topic_id: 28319 reply_id: 114473[/import]

I <3 PHP what can I say :slight_smile:

Oh oh oh… I see your problem…

--define body  
local myBody = { level=1, gametype = 5, speed = 3, score = 100 };  
local jsonBody = json.encode(myBody)  
   
--add header/body to myParams  
local myParams = {}  
myParams.headers = headers  
myParams.body = myBody  

Shouldn’t myParams.body = jsonBody?
[import]uid: 19626 topic_id: 28319 reply_id: 114477[/import]

I’ve changed my code since then so not sure if that was a problem with me reformatting it for the forums or not… It probably was the issue as it seems whenever I get stuck on anything for an extended period of time it’s typically a silly typo like that! [import]uid: 147305 topic_id: 28319 reply_id: 114479[/import]