【发布时间】:2021-03-09 02:37:27
【问题描述】:
我有一个接收此信息的 API 端点:
{
"method": "POST",
"path": "/",
"query": {},
"headers": {
"x-forwarded-for": "xxx.xxx.xxx.xx",
"x-forwarded-proto": "https",
"x-forwarded-port": "443",
"host": "xxx",
"x-amzn-trace-id": "xxx",
"content-length": "128",
"accept": "text/plain, application/json, application/*+json, */*",
"user-agent": "xxx",
"content-type": "application/json;charset=UTF-8",
"accept-encoding": "gzip,deflate"
},
"bodyRaw": "{\"registrations\":[{\"userId\":\"xxx\",\"userAccessToken\":\"550a3a10-a3be-4784-89e2-42e7c8865883\"}]}",
"body": {
"registrations": [
{
"userId": "xxx",
"userAccessToken": "550a3a10-a3be-4784-89e2-42e7c8865883"
}
]
}
}
我无法提取这两个参数。我在 PHP 中使用此代码:
$data = json_decode(file_get_contents('php://input'),true);
$userID = $data['registrations']['userId'];
$userToken = $data['registrations']['userAccessToken'];
然后我将$userID和$userToken这两个变量写入数据库;但是,这两个变量都是空的
我错过了什么?
【问题讨论】:
-
file_get_contents('php://input')的转储是什么? -
基于您的 json,例如路径
userId将是:echo $data['body']['registrations'][0]['userId']; -
不应该是
$data['body']['registrations'][0]['userId'];和$data['body']['registrations'][0]['userAccessToken'];吗? (正文索引)
标签: php json post file-io file-get-contents