1

I am using PHP with XAMPP and Dialogflow to create a chat interface. In a simple intent(question) in Dialogflow, I have created a webhook to XAMPP regarding the question 'Who is X' (e.g. Paul, George). Therefore , I place a POST REQUEST in order to have access to the json form of this question in DIalogflow so that I can answer it as I want to. Specifically, the ultimate goal of this is to retrieve some data from a MySQL database in phpMyAdmin about this question and respond for example that 'X is a developer' or 'X is a financial analyst'. This is why wrote a php script which is the following:

<?php

$method = $_SERVER['REQUEST_METHOD'];

// Process when it is POST method
if ($method == 'POST') {
    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);

    $text = $json->result->parameters;

    switch($text) {
        case 'given-name':
            $name = $text->given-name;
            $speech = $name . 'is a developer';
            break;
        default:
            $speech = 'Sorry I did not get this. Can you repeat please?';
    }       

    $response = new \stdClass();
    $response->speech = "";
    $response->displayText = "";
    $respone->source = "webhook";
    echo json_encode($response);

}
else
{
    echo "Method not allowed";
}

?>

However, the output of this program is: Method not allowed.

Paradoxically enough $method has the value 'GET' so it identifies a GET REQUEST while Dialogflow explicitly states at the webhook page that

Your web service will receive a POST request from Dialogflow in the form of the response to a user query matched by intents with webhook enabled.

Hence I am wondering: why my php script cannot see and process the POST REQUEST from Dialogflow?

P.S. Questions close to mine are the following: Form sends GET instead of POST, Why is $_SERVER['REQUEST_METHOD'] always GET?.

Outcast
  • 4,967
  • 5
  • 44
  • 99

2 Answers2

0

It doesn't work because $_SERVER['REQUEST_METHOD'] == "GET" by default. So you program execute the 'else' condition.

You need to submit a request with the POST method to change this value.

You can use

<form method="POST">
    [...]
</form>

in your HTML, or

$.ajax({
        url : "ajax_url.php",
        type : 'POST',
        data : 'data='+data,
        [...]
    });

in your AJAX JS code for example

Thrilly
  • 27
  • 1
  • Thank you for your response(upvote). However, I do not understand directly where exactly to place this additional source code in my initial source code. Can you be a bit more clear about this? – Outcast Mar 09 '18 at 11:10
  • Yes ! Can you tell me why you need a POST request method to execute your program please ? – Thrilly Mar 09 '18 at 11:13
  • Yes, I also edited my post to include this. I place a POST REQUEST in order to have access to the json form of the question in DIalogflow (the question stated by the user in DIalogflow) so that I can answer it as I want to. – Outcast Mar 09 '18 at 11:17
  • Specifically, the ultimate goal of this is to retrieve some data from a MySQL database in phpMyAdmin about this question and respond for example that 'X is a developer' or 'X is a financial analyst'. – Outcast Mar 09 '18 at 11:20
  • If this is being sent from Dialogflow, there is no web page involved, so there can't be a
    tag.
    – Prisoner Mar 09 '18 at 16:09
  • Yes, exactly there is page involved so I figured it that must simply host it to a proper online server than a self-hosting server like xampp... – Outcast Mar 09 '18 at 16:11
0

Here i am doing same like you from below code your Query will be resolved,

index.php

<?php

require 'get_enews.php';

function processMessage($input) {
    $action = $input["result"]["action"];
    switch($action){

        case 'getNews':
            $param = $input["result"]["parameters"]["number"];
            getNews($param);
            break;

        default :
            sendMessage(array(
                "source" => "RMC",
                "speech" => "I am not able to understand. what do you want ?",
                "displayText" => "I am not able to understand. what do you want ?",
                "contextOut" => array()
            ));
    }
}
function sendMessage($parameters) {
    header('Content-Type: application/json');
    $data = str_replace('\/','/',json_encode($parameters));
    echo $data;
}
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input["result"]["action"])) {
    processMessage($input);
}
?>

get_enews.php

<?php
function getNews($param){
    require 'config.php';
    $getNews="";
    $Query="SELECT link FROM public.news WHERE year='$param'";
    $Result=pg_query($con,$Query);
    if(isset($Result) && !empty($Result) && pg_num_rows($Result) > 0){
    $row=pg_fetch_assoc($Result);
    $getNews= "Here is details that you require - Link: " . $row["link"];
        $arr=array(
            "source" => "RMC",
            "speech" => $getNews,
            "displayText" => $getNews,
        );
        sendMessage($arr);
    }else{
        $arr=array(
            "source" => "RMC",
            "speech" => "No year matched in database.",
            "displayText" => "No year matched in database.",
        );
        sendMessage($arr);
    }
}
?>

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45