Send JSON and parse in PHP

You must Login before you can answer or comment on any questions.

Hi !

I'm searching for days now and i can't find the mistake, i send my json :

var xhr = Ti.Network.createHTTPClient();
xhr.open("POST", "http://****.php");
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
 
var postData = {
    object: { child1:'value', child2:'value' }
}
xhr.send(JSON.stringify(postData));
And in my php, i tried :

$data = json_decode(stripslashes($_POST['postData']), true);

$data = json_decode(stripslashes($_POST['object']), true);

$data = json_decode($_POST['postData']);

$data = json_decode($_POST['object']);

But $data is always null, where did i fail ?

TY

5 Answers

Accepted Answer

Try xhr.send({myData:JSON.stringify(postData)}); and $data = json_decode($_POST['myData']);

— answered 10 months ago by Shannon Hicks
answer permalink
4 Comments
  • There's no reason that I can think of to unnecessarily stringify and decode postData when you can just send the object directly.

    — commented 10 months ago by Jason Priebe

  • I have to vote this one back up simply on premise. This answers the OPs question in the context in which it was asked.

    Lets not forget that our primary goal here is to first answer the question that the OP asked. If the answer is not the best solution, then we offer a better solution. Only by seeing both solutions can the OP really understand which is the better solution.

    In this case, the question was how to send and parse json in php. Even though it may not be the logical thing to do, we should first answer the question, then offer a more logical solution.

    — commented 10 months ago by Anthony Decena

  • Anthony hit the nail on the head. Jason, for all you know the OP is using something like MongoDB on the backend, and is just using PHP as the middleman in keeping sync between a mobile database and a local one.

    — commented 10 months ago by Shannon Hicks

  • Show 1 more comment

Problem 1: don't stringify your POST data, just send it directly, like this:

var xhr = Ti.Network.createHTTPClient();
xhr.open("POST", "http://****.php");
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
 
var postData = {
    object: { child1:'value', child2:'value' }
}
xhr.send(postData);
Problem 2: the server won't see anything called 'postData'. That's just a variable name in your javascript. The server script's $_POST data will look like this:
object: {
    child1 = value;
    child2 = value;
}
So to use it, you would just do this:
$child1 = $_POST['child1'];
$child2 = $_POST['child2'];

— answered 10 months ago by Jason Priebe
answer permalink
3 Comments
  • Hi Jason !

    TY for your answer but this is not working. It only works if i do :

    xhr2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    and :

    var postData = {
        child1:'value1', child2:'value2' 
    }

    And this is not what i want, i want to pass objects with parameters (json or not now i don't really care), but i have thousands objects with 6-7 parameters and i thought json was the best solution.

    — commented 10 months ago by Maxime Charruel

  • I don't understand the problem. Let's look at what happens if we send a more complex object (or an array of objects)

    var xhr = Ti.Network.createHTTPClient();
    xhr.open("POST", "http://****.php");
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
     
    var postData = {
        objectlist: [
            {
                id: 1,
                label: 'one'
            },
            {
                id: 2,
                label: 'two'
            }
        ]   
    }
     
    xhr.send(postData);
    php:
    print_r ($_POST);
    results in
    Array
    (
        [objectlist] => (
            {
            id = 1;
            label = one;
        },
            {
            id = 2;
            label = two;
        }
    )
    )
    Doesn't this do exactly what you're talking about?

    — commented 10 months ago by Jason Priebe

  • Strike this -- see my last response to Shannon's answer. For anything more than scalar values, Shannon's technique is the right one. You have to JSON encode the object.

    — commented 10 months ago by Jason Priebe

Jason is right, also var_dump($_POST); is your choice to check the passed data before trying to access it

TY Jason, Shannon and Anthony, this was nice. Sorry for late answer but i wanted to test with and without json and i finally used Shannon solution : $data = json_decode($_POST['myData'], true);

The second parameter return this output instead of Jason output :

array(1) {
  ["objectiflist"]=>
  array(2) {
    [0]=>
    array(2) {
      ["id"]=>
      int(1)
      ["label"]=>
      string(3) "one"
    }
    [1]=>
    array(2) {
      ["id"]=>
      int(2)
      ["label"]=>
      string(3) "two"
    }
  }
}
And then, to get my data : $data['objectiflist'][0]['id'];

TY everybody for your help ;)

In my PHP (5.4) page I use the function file_get_contents to read post data, otherwise PHP don't see nothing.

$aRequest = json_decode(file_get_contents('php://input'), true);

Your Answer

Think you can help? Login to answer this question!