3
<?php

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

print_r($data);
?>

Output given is {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

Json Data posted is:

 {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

Writing the json data in a variable and passing it to json_decode works but posting the same from the "php://input" returns a JSON data instead of associative array.

7
  • what does var_dump($json) print? Commented Nov 21, 2016 at 20:09
  • Yes, It prints this value string(107) ""{\"EventTitle\":\"Game\",\"EventBody\":\"body\",\"EventDate\":\"20 November, 2016\",\"EventType\":\"party\"}"" Commented Nov 21, 2016 at 20:12
  • What happens if you change file_get_contents('php://input',true); to file_get_contents('php://input');? Commented Nov 21, 2016 at 20:12
  • 4
    looks like it was encoded twice Commented Nov 21, 2016 at 20:13
  • Same Output as before {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"} Commented Nov 21, 2016 at 20:14

1 Answer 1

9

It looks like @tkausl is correct. The JSON you're receiving has been double-encoded. Since it's double-encoded, a temporary solution would be to double-decode it.

$data = json_decode(json_decode($json), true);

But the real solution is to figure out why it's like that to begin with and fix it (if it's yours to fix).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.