我正在将对象从 fiddler 传递到用 codeigniter 编写的服务。我的对象是这样的:
我的回复:
{
"GetResponse":{
"OrgName":"adfasd",
"OrgAdr1":"asdf",
"OrgAdr2":"NA",
"ProductList":[
{
"ProductID":8724,
"Pallets":0,
"Pieces":0,
"Description":"sdfsd"
}
]
}
}
我想要什么:
我想在 codeigniter 中将响应保存为 JSON 对象,还想获取主对象中的 JSON 对象或数组。
我尝试了什么:
我在 codeigniter 中的服务方法是这样的:
public function Save()
{
$Data = json_decode(file_get_contents('php://input'));
echo $Data;
}
但是我在 Fiddler 中的 Response Body 中什么也得不到。
如果我使用这段代码:
$Data = file_get_contents('php://input');
echo $Data;
然后它以字符串的形式向我显示响应。我想将其保存为 JSON 对象。
谁能告诉我我在这里错过了什么?
最佳答案
使用这段代码:
header('Content-type: application/json');
$Data = json_decode(file_get_contents('php://input'),true);
现在,您将获得数组形式的$Data
。
然后您通过 $Data['name']
获得值(value)。
关于php - 在 Codeigniter 中获取 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027205/