Submitting JSON via POST in PHP?


I’m working on a project that involves a kiosk submitting JSON data via POST. While I’ve received JSON data and successfully parsed it I’ve never sent JSON data via post.

I have to submit a json object that contains a client’s address, name etc… to a web address. Once that JSON object is received on the other end it will be parsed and read into a database.

I’m aware of the json_encode, json_decode aspect. So here’s my questions:

  1. How do I go about submitting the json object? Do I simply add it to a post element? For instance will I have a post element that is something like:

“$_POST[jsonOBJ]” and I put my JSON into that POST element?

  1. Since JSON is essentially text can I simply submit it as a string?

There are lots of great tutorials out there on consuming JSON from a source but very little instructing people how to send JSON from PHP.

Thanks in advance! :slight_smile:

It’s permissible to set a POST variable to any string, including a JSON-encoded string. I usually break the object up into separate POST variables though. You can turn it back into an object of stdClass in PHP like this:

$obj = (object) $_POST;

Hey thanks for the quick reply!

Hmm that’s interesting the programmers I’m working with are insisting it be sent as a complete JSON object. So I’m more inclined to place the JSON object into a post array if possible. (Which it sounds like I can).

Personally I like your method better however, probably because it follows convention and also because I’m more familiar with it. heh

Any other advice, tips, help is appreciated! :slight_smile:

I’m sure you have your reasons, but why do you need to submit the data as JSON? Is there something wrong with using a normal form POST and dealing with it through the $_POST array?

For what they want you to do, you’ll probably need to extend the Object prototype (or your custom object’s prototype) with a toJsonString() method that converts the object to a JSON string. That would entail about the same amount of processing as converting it to individual POST variables, which process consists of building a query string and putting it in the request body. No real difference either way.

I use the stdClass coercion in PHP to build an object from an array before JSON-encoding it because JavaScript has no notion of a hash or associative array - therefore it makes little sense to send a hash response to JavaScript.

I’m working with another development team and they claim its easier for them to make changes later if they receive a JSON submission.

Personally I think it’s a better choice to go the simplest route which would be a POST submission but I’m trying to determine how much work it is to submit a JSON object. Something I’ve never done before.

It’s roughly the same amount of work either way on the client side: you build a string of delimited property names and values (either a JSON string or a URL-encoded query string).

That’s true. Although we already have most of it as a post submission so their request has me wondering why the sudden push. But I digress… heh

Charge them extra for changing the spec after you’ve already started coding? :smiley:

I wish although we ar both suppliers to one client and he’s the one making the changes. heh.

Couple more questions about this process. Would something like this work:

  1. Create the array of associated data, I’ll use the example from php docs:
    $arr = array (‘a’=>1,‘b’=>2,‘c’=>3,‘d’=>4,‘e’=>5);

  2. Encode the array as a JSON object and save it to a value:
    $jsonOBJ = json_encode($arr)

  3. Place the JSON object directly into a post array and send it off:
    $_POST[‘jsonResp’] = $jsonOBJ

This is just an example to show my train of though it’s not meant to be code you can run. Also I’m using curl to submit the post data so I’ll add the insertion of JSON into post then.


There’s no such thing as an associative array in JavaScript, so coerce the array to a stdClass object before you JSONify it:

$arr = (object) array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

Ah ok it’s starting to make sense, here’s another wrench to the whole discussion. The kiosk interface is flash based. So the process is something like so:

actionscript submits post array (via ajax call) to —> php and curl —> curl then sends to client website for processing.

My challenge is getting the json respons preped and sent. I’m thinking I’ll send a set of regular POST elements to php from actionscript since the tools to create and manipulate JSON in actionscript a very limited.

I’ll then use PHP to JSONify (using json_encode) the post elements and then curl will send the json object off. Does that make sense to anyone or am I nuts?

(Jokes on the latter will be ignored :wink: heh )

Makes sense. I would further url_encode the JSON string before sending it to the client as a single POST (or GET) variable via CURL, to avoid the possibility of it being misinterpreted. If the client is using PHP, their script will automatically reverse the URL encoding.

Makes sense to me.

FLASH posts to PHP.
PHP receives post array, converts to JSON, and sends to external website via cURL.

In the PHP script, take the elements you need, put them in another array, and json_encode it. Send the json string via cURL. Its actually pretty simple.

Awesome thanks guys! You’ve been a big help.