PHP script receives GET instead of POST REQUEST

2019-07-21 05:35发布

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?.

2条回答
爷的心禁止访问
2楼-- · 2019-07-21 05:52

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

查看更多
叼着烟拽天下
3楼-- · 2019-07-21 06:09

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

查看更多
登录 后发表回答