使用php,如何从dialogflow实现JSON请求中获取特定数据并将数据存储到php变量中

时间:2018-11-12 19:10:23

标签: php json api dialogflow fulfillment

使用php,我如何从对话流实现JSON请求中获取特定数据并将其存储到php变量中?

这是我从dialogflow代理得到的JSON响应

{
  "responseId": "e9106589-2a41-47c4-bdde-8f3cee8f40da",
  "queryResult": {
    "queryText": "switch on cfl",
    "action": "input.switchoncfl",
    "parameters": {
      "makeRequest": "cfl_on"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "platform": "ACTIONS_ON_GOOGLE",
        "simpleResponses": {
          "simpleResponses": [
            {
              "textToSpeech": "Sure. Turning CFL on... Anything else can I help you with?",
              "displayText": "Sure. Turning CFL on..."
            }
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/smarthome-cf277/agent/intents/5be27f38-105d-4854-b62d-ec3a6de80cc7",
      "displayName": "1.1-Switch_on_CFL"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "payload": {}
  },
  "session": "projects/smarthome-cf277/agent/sessions/ca0261aa-913f-96f1-b5aa-7755637d5ab7"
}

这是我的php代码

<?php
    header("Content-Type: application/json");
    ob_start();
    $content = json_decode(file_get_contents('php://input'),true);
    $action = $content['parameters']['makeRequest'];
    ob_end_clean();
?>

我想将 cfl_on 值存储到 $ action 变量中。我怎样才能做到这一点?此php代码不起作用。

1 个答案:

答案 0 :(得分:0)

您只是错过了对象的“ queryResult”层。

$action = $content['queryResult']['parameters']['makeRequest'];