0

I am facing error while getting response from JSON array. here is PHP code I try this code.

var_dump($_POST);die;

empty

$data = (array) json_decode($HTTP_RAW_POST_DATA, true);
var_dump($data);

empty

$arJson =(array) json_decode( $_POST, true );
var_dump($arJson); 

this one also empty here is postman results.

enter image description here enter image description here

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Darkcoder
  • 802
  • 1
  • 9
  • 17

3 Answers3

2

If your whole POST body contains the JSON, you can get it using thid piece of code:

$json = file_get_contents('php://input');
$decoded = json_decode($json);
Kyoya
  • 343
  • 2
  • 5
  • bro its api . how can store $_post array in json file – Darkcoder Mar 02 '17 at 08:03
  • 1
    If I understood yor comment correctly, you want to put the JSON encoded POST data into a file: `file_put_contents('/path/to/file.json', $_POST)` – Kyoya Mar 02 '17 at 08:20
2

Try this: $postData = json_decode(file_get_contents("php://input"));

If you simply POST a good old HTML form, the request looks something like this:

POST /page.php HTTP/1.1

key1=value1&key2=value2&key3=value3

But if you are working with Ajax a lot, this probaby also includes exchanging more complex data with types (string, int, bool) and structures (arrays, objects), so in most cases JSON is the best choice. But a request with a JSON-payload would look something like this:

POST /page.php HTTP/1.1

{"key1":"value1","key2":"value2","key3":"value3"}

The content would now be application/json (or at least none of the above mentioned), so PHP's $_POST-wrapper doesn't know how to handle that (yet).

The data is still there, you just can't access it through the wrapper. So you need to fetch it yourself in raw format with file_get_contents('php://input') (as long as it's not multipart/form-data-encoded).

Rahul Patel
  • 639
  • 6
  • 12
1

You can get value like this:

$str = file_get_content("php://input");
$data = json_decode($str,true);

Hope you can help.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
itbirds
  • 9
  • 2