user2005049
user2005049

Reputation: 550

PHP POST method json_decode return NULL

I'm using sencha touch and I'm sending data to php REST server to save it to database, in firebug I can see the parameters that sencha touch send to php side, but in php I have this code:

parse_str(file_get_contents("php://input"),$post_vars);
$info=$post_vars['customers'];
$data=json_decode(stripslashes($info),true);

The json_decode return NULL, the get_magic_quotes_gpc is off I also tried utf8_encode but always I got NULL, I tried var_dump and at the response I got extra text:

    array(1) {
     ["customers"]=>
     string(50) "{"c_name":"test","c_tel":"08-05852821","id":"112"}"
    }

I don't know how to continue, before the var_dump the post contains:

{"success":{"customers":"{\"c_name\":\"test\",\"c_tel\":\"08-05852821\",\"id\":\"112\"}"}}

I tried stripslashes but I got also NULL...

Any idea...

Upvotes: 0

Views: 711

Answers (2)

Alexander
Alexander

Reputation: 20234

There are three possibilities how Jason can be transmitted.

  • First, it can be a get variable with a Name (index.php?data={success:true}), this only Works for Short json strings.
  • Second, it can be a post variable with a Name ( will be Sent as x-www-form-urlencoded)
  • Third, you can post only json (Store.sync does that)

Only the Latter will be accessed via php://Input, it Seems as of you use the Second.

Upvotes: 0

jeroen
jeroen

Reputation: 91762

Based on your comment, I would access $_POST directly:

$info = json_decode($_POST['customers'], true);
echo $info['c_name'];

Upvotes: 1

Related Questions