【问题标题】:using php, how to fetch a specific data from dialogflow fulfillment JSON request and store the data into a php variable使用 php,如何从 dialogflow 实现 JSON 请求中获取特定数据并将数据存储到 php 变量中
【发布时间】:2018-11-12 19:10:22
【问题描述】:

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

这是我从对话流代理获得的 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 代码不起作用。

【问题讨论】:

  • 在 php 端使用 var_dump($_POST[])exit;,然后将发出请求的 javascript 指向它。发出请求后,它将在元素检查器的网络选项卡中创建一个条目,您可以通过查看它来调试服务器端,或者只是通过 url 公开端点并在浏览器中调试。

标签: php json api dialogflow-es fulfillment


【解决方案1】:

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

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

【讨论】:

  • 没问题。下次你只需要更仔细地看一下数据结构!
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 2014-12-14
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 2020-07-06
相关资源
最近更新 更多