简体   繁体   English

我如何使用php中的post方法从json数组获取响应

[英]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.

相关问题 如何使用 PHP 从 curl POST 返回的 JSON 响应的一部分 - How can I get part of a JSON response that is returned from a curl POST using PHP 如何使用POST方法从Android获取php的Json DATA? - How to get Json DATA with php from Android using POST method? 如何使用AJAX发布方法从PHP的特定函数中检索JSON - How can I retrieve JSON from PHP's specific function using AJAX post Method 如何使用 php 从多维 json 响应中获取键和值 - how can I get the key and value from a multidimensional json response using php 我如何使用php获取json对象的数组? - How can i get the array of json object using php? 如何在 PHP 中使用 POST 从 CKEDITOR 获取值? - How can I get the values from CKEDITOR using POST in PHP? 如何使用jQuery的ajax方法将json数组发布到PHP? - How to post json array to PHP using jQuery's ajax method? 带有响应array []的jQuery Ajax PHP POST方法instad发送了数据。 如何获得发送值作为响应? - jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response? PHP - 如何使用get或post方法抓取外部URL并从中解析json数据? - PHP - how to crawl external url using get or post method and parse json data from it? 如何从 influxdb 的数组 JSON 响应中获取 PHP 值 - How get in PHP value from array JSON response of influxdb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM