PHP cannot extract JSON from HTTP POST

  file-get-contents, file-io, json, php, post

I have an API endpoint which receives this information:

{
  "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"
      }
    ]
  }
}

I cannot extract the two parameters. I use this code in PHP:

$data = json_decode(file_get_contents('php://input'),true);
$userID = $data['registrations']['userId'];
$userToken = $data['registrations']['userAccessToken'];

I then write the two variables $userID and $userToken to database; however, both variables are empty

What am I missing?

Source: Ask PHP

LEAVE A COMMENT