[英]how i can get response from json array using post method in php
I am facing error while getting response from JSON array. 从JSON数组获取响应时遇到错误。 here is PHP code I try this code. 这是PHP代码,我尝试使用此代码。
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: 如果整个POST正文都包含JSON,则可以使用以下代码段获取它:
$json = file_get_contents('php://input');
$decoded = json_decode($json);
Try this: $postData = json_decode(file_get_contents("php://input"));
试试这个: $postData = json_decode(file_get_contents("php://input"));
If you simply POST a good old HTML form, the request looks something like this: 如果您只是简单地发布旧的HTML表单,则请求看起来像这样:
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. 但是,如果您经常使用Ajax,那么这种可能性还包括使用类型(字符串,整数,布尔值)和结构(数组,对象)交换更复杂的数据,因此在大多数情况下,JSON是最佳选择。 But a request with a JSON-payload would look something like this: 但是带有JSON有效负载的请求看起来像这样:
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). 内容现在将是application / json(或至少没有上述提到的内容),因此PHP的$ _POST-wrapper不知道该如何处理。
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
). 因此,您需要使用file_get_contents('php://input')
以原始格式自己获取(只要它不是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. 希望能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.