【发布时间】:2020-06-21 10:11:41
【问题描述】:
我正在制作一个小型 API 来处理一些调查。
我有以下身体:
{
"name":"1: asd",
"children":[
{
"name":"2: are",
"children":[
{
"name":"3: wat wat",
"children":[
{
"name":"4: in da hut",
"context":{
"question":"in da hut",
"questionType":"rbText",
"answers":[
{
"value":"",
"index":0,
"indexValue":1
}
]
}
},
{
"name":"5: k k k k",
"context":{
"question":"k k k k",
"questionType":"rbText",
"answers":[
{
"value":"",
"index":0,
"indexValue":1
}
]
}
}
],
"context":{
"question":"wat wat",
"questionType":"rbMultiple",
"answers":[
{
"value":"sim",
"index":2,
"indexValue":4
},
{
"value":"nao",
"index":3,
"indexValue":5
}
]
}
}
],
"context":{
"question":"are",
"questionType":"rbMultiple",
"answers":[
{
"value":"potatoes",
"index":4,
"indexValue":3
},
{
"value":"nay",
"index":4,
"indexValue":3
}
]
}
}
],
"context":{
"question":"asd",
"questionType":"rbText",
"answers":[
{
"value":"",
"index":5,
"indexValue":2
}
]
}
}
在 php 方面,为了测试请求是否成功,我尝试了以下方法:
echo $_POST['name'];
但我收到以下错误:
注意:未定义索引:名称在 C:\xampp\htdocs\LimeAPI\api\objects\create.php上线 15
所以我添加了
var_dump($_POST)
在数组中正确打印我的请求。
所以我更改了代码以遍历数组并打印元素:
foreach($_POST as $item) {
echo $item;
var_dump($item);
}
但现在我得到以下信息:
<b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\LimeAPI\api\objects\create.php</b> on line <b>17</b><br /> Arrayarray(1) { ["
{
"name":"2: are",
"children":[
{
"name":"3: wat wat",
"children":[
{
"name":"4: in da hut",
"context":{
"question":"in da hut",
"questionType":"rbText",
"answers":[
{
"value":"",
"index":0,
"indexValue":1
}
"]=> string(0) "" }
我做错了什么?
提前谢谢你
【问题讨论】:
标签: php post httprequest