<?php // First, include Requests include('../library/Requests.php'); // Next, make sure Requests can load internal classes Requests::register_autoloader(); $qu = htmlspecialchars(trim($_POST["qu"])); $headers = array('Content-Type' => 'application/json'); $data = array('scene_id' => 5045, 'query' => $qu, 'session_id' => ""); /** * 获取Access_token * @param string $url * @param string $param * @return bool|mixed */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($curl);//运行curl curl_close($curl); return $data; } $url = 'https://aip.baidubce.com/oauth/2.0/token'; $post_data['grant_type'] = 'client_credentials'; $post_data['client_id'] = '5QX6Ntb*******tvFInzGRn'; $post_data['client_secret'] = 'ZfZU5XbB********E8mAIzaviLOI'; $o = ""; foreach ($post_data as $k => $v) { $o .= "$k=" . urlencode($v) . "&"; } $post_data = substr($o, 0, -1); $res = request_post($url, $post_data); $arr = json_decode($res, true); $access_token = $arr["access_token"]; $url = "https://aip.baidubce.com/rpc/2.0/solution/v1/unit_utterance?access_token=$access_token"; // Now let's make a request! $request = Requests::post($url, $headers, json_encode($data)); $xx = json_decode($request->body, true); //echo $xx["result"]["action_list"][0]["say"]; //echo $xx["result"]["action_list"][0]["main_exe"]; //后台返回的执行函数标示 $func = $xx["result"]["action_list"][0]["main_exe"]; function xy_func($str) { define("HOST","xxx"); //主机名 define("USER","xxxx"); //账号 define("PASS","xxxxx"); //密码 define("DBNAME","xxxxxx"); //数据库名 $link = mysql_connect(HOST, USER, PASS); mysql_select_db(DBNAME); $sql = "SELECT id,fileName,fileIntro,imageUrl FROM xxx WHERE id>905 ORDER BY RAND() LIMIT 1"; $res=mysql_query($sql); $a = array(); while($row=mysql_fetch_assoc($res)){ global $a; $a[] =$row; } } if (function_exists("$func")) { $func($qu); } else { } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>小雨在线-</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style type="text/css" media="screen"> </style> </head> <body> <p></p> <div class="container"> <form class="form-inline" action="" method="post"> <div class="form-group"> <label class="sr-only" for="question">输入您问的问题?</label> <input type="text" class="form-control" id="question" name="qu" placeholder="用测试样本做测试"> </div> <button type="submit" class="btn btn-danger">解惑</button> <hr> <h4 style="color: red">对话样本:</h4> <p>什么是一次函数</p> <p>一次函数是什么</p> <p>一次函数是啥</p> <p>啥叫一次函数</p> <hr> <h4 style="color: red">以下为bot回应内容:【这是模拟从后台数据库返回的富文本数据】</h4> <p> <?php if (isset($a)) { foreach ($a as $key =>$v) { echo $v["fileName"]."<br>"; echo $v["fileIntro"]."<br>"; echo "<img width='400' src=".$v["imageUrl"]." >"; } } else { echo "bot不知道应该怎么答复您,请重新来一遍!"; } ?> </p> <hr> <h4 style="color: red">代码调试区:</h4> <?php echo "<pre>"; print_r($xx); echo "</pre>"; ?> </form> </div> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html>
转载于:https://my.oschina.net/yjft/blog/1358482