0

I'm trying send a JSON to PHP. In PHP Script I'm trying get this JSON using $_POST but doesn't works, when I try use echo $json does not display value. To test if PHP Script is works I'm using RESTClient of Mozilla Firefox.

How could I send a JSON to PHP using $_POST ?

JSON

{
"jsondata":{
    "nome":"my name"
}}

PHP

<?php
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');

$jsondata = $_POST['jsondata'];
$data = json_decode($jsondata, true);
print_r($data);

?>

Exception

<br />
<b>Notice</b>:  Undefined index: jsondata in <b>/opt/lampp/htdocs/TelefonesUteis/ws/add_estado.php</b> on line <b>6</b><br />

Line 6 have->$data = json_decode($jsondata, true);

enter image description here

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • That is not valid JSON - you have a `}` missing. –  Feb 17 '15 at 04:49
  • Can you please share the entire code? Also json seems to be invalid – Siddharth Patel Feb 17 '15 at 04:49
  • Are you aware that [`json_decode` will return `NULL` if presented with invalid JSON?](http://php.net/manual/en/function.json-decode.php#refsect1-function.json-decode-returnvalues) – sjagr Feb 17 '15 at 04:52
  • And yet your screenshot indicates you're still using incorrect JSON. – sjagr Feb 17 '15 at 04:54
  • @sjagr why u marked my post as duplicate ? – FernandoPaiva Feb 17 '15 at 04:58
  • @Phil did, and he's quite right. Review the duplicate for your answer. The accepted answer will solve your problem. You should do some research before posting your own problem to see if others have experienced the same issue (they likely have.) – sjagr Feb 17 '15 at 04:59
  • @FernandoPaiva probably because it's a duplicate. – hobbs Feb 17 '15 at 05:00

0 Answers0