如何解决在对话框流中显示来自PHP文件的查询结果
所以我正在使用dialogflow创建一个聊天机器人。我将代码编写在php文件中,并且尝试从数据库中获取数据。与数据库的连接有效,查询返回的结果放入变量$ ppp中,我使用定义的sendMessage函数回显结果,唯一的问题是我只能回显请求的第一个结果似乎while循环无法正常工作,或者sendMessage函数有问题,我不知道该怎么办。
<?php
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set("Asia/Bangkok");
$date = date("Y-m-d");
$time = date("H:i:s");
$json = file_get_contents('php://input');
$request = json_decode($json,true);
$input = fopen("log_json.txt","w") or die("Unable to open file!");
fwrite($input,$json);
fclose($input);
function processMessage($update) {
if($update["queryResult"]["action"] == "ques"){
$bdd= new PDO('mysql:host=localhost;dbname=*****','*****',array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) ;
$nom= $update["queryResult"]["parameters"]["nom_aliment"];
$info=$update["queryResult"]["parameters"]["Information"];
$quantite=$update["queryResult"]["parameters"]["Quantite"];
$req=$bdd->query("SELECT * FROM TableCiqual");
$req=$bdd->prepare("SELECT * FROM TableCiqual WHERE alim_nom LIKE ? ");
$req->execute(array("%$nom%"));
while($resultat=$req->fetch()){
$variab=$resultat[$info]*$quantite/100;
$ppp =$resultat['alim_nom'].' '.$info.' : '.$variab;
sendMessage(array(
"source" => $update["responseId"],"fulfillmentText"=>$ppp,"payload" => array(
"items"=>[
array(
"simpleResponse"=>
array(
"textToSpeech"=>"Bad request"
)
)
],),));
//}
}
}
function sendMessage($parameters) {
echo json_encode($parameters);
}
$update_response = file_get_contents("php://input");
$update = json_decode($update_response,true);
if (isset($update["queryResult"]["action"])) {
processMessage($update);
}else{
sendMessage(array(
"source" => $update["responseId"],"fulfillmentText"=>"Hello from webhook",));
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。