如何使用PHP在api.ai中使用webhook从外部获取数据库?

时间:2018-02-06 22:11:27

标签: php database heroku google-cloud-storage api-ai

我在API.AI中创建了代理,我在Google Cloud Platform中创建了一个数据库。我可以使用Navicat Premium连接到它来更新机器人的数据库。此外,我可以使用Heroku和PHP脚本使用webhook。我可以通过以下代码成功获得回复

<?php 


$method = $_SERVER['REQUEST_METHOD'];

// Process only when method is POST
if($method == 'POST'){

    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);
    $intent = $json->result->metadata->intentName;
    $entertype = $json->result->contexts->entertainmentType.original;

    $response = array(
                      "messages"=> [array(
                                       "type"=> 1,
                                       "platform"=> "facebook",
                                       "title"=> 'MBO Melaka Mall',
                                       "subtitle"=> 'Tingkat 2, Kompleks Melaka Mall, Lebuh Air Keroh, 75450 Melaka',
                                       "imageUrl"=> "http://s-yoolk-images.s3.amazonaws.com/my/gallery_images/medium/1435340336/67323?1435340336",
                                       "buttons"=> [ array(
                                                      "text"=> "Search in Google",
                                                      "postback"=>"https://www.google.com/search?q=mbo+melaka+mall&oq=mbo+melaka+mall&aqs=chrome..69i57j5.6761j0j7&sourceid=chrome&ie=UTF-8"
                                                  )
                                            ]
                                   )]
                        );

    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}

?>

然而,在我更新之后,我无法获得任何响应,并且我收到错误代码500作为内部服务器错误。请帮帮我。

<?php 


$method = $_SERVER['REQUEST_METHOD'];

// Process only when method is POST
if($method == 'POST'){

    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);
    $intent = $json->result->metadata->intentName;
    $entertype = $json->result->contexts->entertainmentType.original;


    $username="root";
    $password="pass";
    $database="qwertytrek";
    $connection=mysql_connect("35.200.241.96",$username,$password);*/
    if(!$connection)
    {
        $speech = "No Connection";
    }
    else 
    {
        $speech = "Connected to Google";
    }
    $selectdb = mysql_select_db($connection,$database) or die("Unable to select database");
    if ($selectdb) 
    {
        //echo = "database selected";
    }


    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}

function cybercafe($user)

{
}
?>

0 个答案:

没有答案