PHP:file_get_contents('php://input') 返回 JSON 消息的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21974951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: file_get_contents('php://input') returning string for JSON message
提问by Dave Moz
I am trying to read in a JSON message in my PHP app and this is my php code:
我试图在我的 PHP 应用程序中读取 JSON 消息,这是我的 php 代码:
$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE);
echo $obj->{'S3URL'};
When I do this I am getting the following error:
当我这样做时,我收到以下错误:
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
This is the request body of the request to the page:
这是对页面的请求的请求正文:
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
"S3URL": "http://url.com"
}
This is the request headers:
这是请求标头:
Accept: application/json
Content-Type: application/json
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML,
However, if I instead echo out the $jsonvariable I get the following:
但是,如果我改为回显$json变量,我会得到以下信息:
S3URL=http%3A%2F%2Furl.com
So it looks like file_get_contents('php://input');is reading it in as a string, and not as JSON, which will make parsing it more difficult.
所以看起来file_get_contents('php://input');它是作为字符串而不是 JSON 来读取的,这会使解析它变得更加困难。
Any idea why it isn't being returned as JSON, or how I can get it to be returned as JSON?
知道为什么它没有作为 JSON 返回,或者我如何让它作为 JSON 返回?
采纳答案by elixenide
Your use of json_decodeis creating an associative array, not an object. You can treat it like an array, instead of an object. If you want an object, use this, instead:
您使用的json_decode是创建一个关联数组,而不是一个对象。你可以把它当作一个数组,而不是一个对象。如果你想要一个对象,请使用这个,而不是:
$obj = json_decode($json);
See the documentationon the second parameter to json_decode():
assoc When TRUE, returned objects will be converted into associative arrays.
assoc 当为 TRUE 时,返回的对象将被转换为关联数组。
Also, as Johannes H. pointed out in the comments, the output of echo $json;indicates that you are not actually receiving JSON, in the first place, so you will need to address that, as well. You asked why it isn't JSON; without seeing how you are requesting this script, it's impossible to say for sure.
此外,正如 Johannes H. 在评论中指出的那样, 的输出首先echo $json;表明您实际上并未收到 JSON,因此您也需要解决这个问题。你问为什么它不是 JSON;没有看到您如何请求此脚本,就无法确定。
回答by AKASH VERMA
there are two type for executing this type of request
有两种类型可以执行这种类型的请求
First : you can use it as an stdClassObject for this
首先:您可以将其用作 stdClassObject
$data = json_decode(file_get_contents('php://input'));
$data = json_decode(file_get_contents('php://input'));
it will return a object and you can retrieve data from this like
它将返回一个对象,您可以从中检索数据,例如
$name = $data->name;
$name = $data->name;
Second : you can use it as an array for this
第二:您可以将其用作数组
$data = json_decode(file_get_contents('php://input'), true);
$data = json_decode(file_get_contents('php://input'), true);
it will return a object and you can retrieve data from this like
它将返回一个对象,您可以从中检索数据,例如
$name = $data['name'];
$name = $data['name'];
回答by Shanaka Madusanka
Try this https://stackoverflow.com/a/56801419/7378998
试试这个https://stackoverflow.com/a/56801419/7378998
It works for JSON or jquery post
它适用于 JSON 或 jquery 帖子
$.post(url, $('form').serialize());
回答by Ansh Mehta
If you are using javascript to send JSON, to a php file
如果您使用 javascript 将 JSON 发送到 php 文件
send_recieve.js
发送接收.js
var myObject = JSON.stringify({"name":"John","age":30,"city":"New York"});
var xhr = new XMLHttpRequest();
xhr.open("POST","http://localhost/dashboard/iwplab/j-comp/receive_send.php",false);
xhr.setRequestHeader("Content-type","application/json");
xhr.onreadystatechange = function(){
if(xhr.readyState==4){console.log("xhr response:"+xhr.response)}
alert(xhr.responseText);
};
xhr.send(myObject);
recieve_send.php
接收_发送.php
<?php
$var = json_decode(file_get_contents("php://input"),true);
echo "Data recieved by PHP file.\n";
if ($var["name"]=="John"){
echo "a";
}
else{
echo "b";
}
//Manipulate/validate/store/retrieve to database here
//echo statements work as response
echo "\nSent";
?>
echo statements work as a response.
echo 语句用作响应。
回答by ramin
$obj = json_decode($json);
$obj = json_decode($json);
Just remove the true
删除真正的
回答by Prithabai R
The problem may be form php://input (is a read-only stream that allows you to read raw data from the request body). change some setting from php.ini , try to make "allow_url_fopen" on.
问题可能是形式 php://input (是一个只读流,允许您从请求正文中读取原始数据)。从 php.ini 更改一些设置,尝试打开“allow_url_fopen”。
回答by Ratanveer Singh
Use this one result will
使用这一结果将
$chd = json_decode(file_get_contents('php://input'), TRUE);
$childs = implode("",$chd);

