JSON Post PHP(TypeForm)

时间:2018-05-22 16:58:22

标签: php json typeform

如果这是一个简单的请求,我从未使用JSON,所以道歉。

我有一个webhook设置,向我发送一个JSON帖子(下面的例子) - 我想从这个"text":"250252"&提取两个答案。 {"label":"CE"}

{
  "event_id": "1",
  "event_type": "form_response",
  "form_response": {
    "form_id": "VpWTMQ",
    "token": "1",
    "submitted_at": "2018-05-22T14:11:56Z",
    "definition": {
      "id": "VpWTMQ",
      "title": "SS - Skill Change",
      "fields": [
        {
          "id": "kUbaN0JdLDz8",
          "title": "Please enter your ID",
          "type": "short_text",
          "ref": "9ac66945-899b-448d-859f-70562310ee5d",
          "allow_multiple_selections": false,
          "allow_other_choice": false
        },
        {
          "id": "JQD4ksDpjlln",
          "title": "Please select the skill required",
          "type": "multiple_choice",
          "ref": "a24e6b58-f388-4ea9-9853-75f69e5ca337",
          "allow_multiple_selections": false,
          "allow_other_choice": false
        }
      ]
    },
    "answers": [
      {
        "type": "text",
        "text": "250252",
        "field": {
          "id": "kUbaN0JdLDz8",
          "type": "short_text"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "CE"
        },
        "field": {
          "id": "JQD4ksDpjlln",
          "type": "multiple_choice"
        }
      }
    ]
  }
}

我目前在我的PHP文件中有这个:

$data = json_decode(file_get_contents('php://input'));
$ID = $data->{"text"};
$Skill = $data->{"label"};

这不起作用,我得到的只是空 - 任何帮助都会非常感激,谢谢。

1 个答案:

答案 0 :(得分:2)

您需要查看您收到的JSON对象,以便了解使用json_decode后您收到的对象的结构,您尝试获取的对象是{ {1}},因此您可以拥有一个变量以便于访问:

$data->form_response->answers

记住$answers = $data->form_response->answers; 是一个数组

为了达到你想要的目的,你可以做到:

$answers