using php, how to fetch a specific data from dialogflow fulfillment JSON request and store the data into a php variable

971 views Asked by At

Using php, how can I fetch specific data from dialogflow fulfillment JSON request and store the data into a php variable?

Here is the JSON response I 'm getting from my dialogflow agent

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

And here is my php code

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

I want to store cfl_on value to $action variable. How can I do that? this php code does not work.

1

There are 1 answers

1
ADyson On BEST ANSWER

You just missed out the "queryResult" layer of the object.

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