【问题标题】:JSON Post PHP (TypeForm)JSON Post PHP (TypeForm)
【发布时间】:2018-05-22 16:58:21
【问题描述】:

我以前从未使用过 JSON,如果这是一个简单的请求,我深表歉意。

我有一个向我发送 JSON 帖子的 webhook 设置(下面的示例) - 我想从 "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"};

这不起作用,我得到的只是空 - 任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 你能var_dump(file_get_contents('php://input'));

标签: php json typeform


【解决方案1】:

在使用json_decode 后,您需要查看接收到的 JSON 对象以了解接收到的对象的结构,您想要获取的是在$data->form_response->answers 中,因此您可以拥有一个便于访问的变量:

$answers = $data->form_response->answers;

记住$answers 是一个数组

所以要实现你想要的,你可以这样做:

$data = json_decode(file_get_contents('php://input'));
$answers = $data->form_response->answers;
$ID = $answers[0]->text;
$Skill = $answers[1]->choice->label;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2011-05-15
    • 2018-09-17
    • 2013-10-01
    相关资源
    最近更新 更多