简体   繁体   中英

how i can get response from json array using post method in php

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.

在此处输入图片说明 在此处输入图片说明

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);

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 ).

You can get value like this:

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

Hope you can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM