php微信公众号开发用户发送关键词,后台查询数据库后返回查询结果的方法
2019-12-17 admin php 2006
原来开发的一个简易php查询系统,采用原生php+mysql,用户导入excel表格后,可设置查询条件,用户可以电脑和手机上查询。
原程序已对接小程序,现在客户需要对接公众号,用户关注公众号后,输入关键词如:1/张三,公众号查询数据库后,直接显示第一张表姓名=张三的查询结果。
需要用到的是微信公众号的文字回复功能。首先在微信公众号开发->基本配置界面,修改服务器配置:
注意Token和下面代码中的define("TOKEN", "my_weixin")对应。php接口文件如下:
header('content-type:text/html;charset=utf-8'); define("TOKEN", "my_weixin"); //define your token require_once("inc/conn.php"); if($_GET['echostr']){ //如果发来了echostr则进行验证 $echoStr = $_GET["echostr"]; //验证字段 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ echo $echoStr; exit; } }else{ //获取项目列表 $sz=array();//项目列表 $i = 0; $data = array();//项目表名 $tiaojian = array();//条件数组 $sql = iconv("utf-8","gbk","select biaoming,tiaojian1,tiaojian2 from bgsx where sfkf = '已开放'"); $check_query = mysqli_query($conn,$sql); while ($row = mysqli_fetch_row($check_query)){ $i++; if($row[2] == ''){ $temp = $i.'.'.$row[0].'/'.$row[1]; }else{ $temp = $i.'.'.$row[0].'/'.$row[1].'/'.$row[2]; } $sz[] = iconv('gbk','utf-8',$temp); $data[] = $row[0]; $tiaojian[] = $row[1]; } //生成查询提示列表 $i=0; foreach($sz as $val){ $i++; if($i == 1){ $list="输入序号+查询条件,用/分割。比如:1/张三\n".$val; }else{ $list = $list."\n".$val; } } mysqli_free_result($check_query); $data = mult_iconv('gbk','utf-8',$data); $tiaojian = mult_iconv('gbk','utf-8',$tiaojian); $datas = array(); $datas['list'] = $list; $datas['data'] = $data; $datas['tiaojian'] = $tiaojian; //如果没有echostr,则返回消息 //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //通过GLOBALS这个全局变量来获取原始的Post数据,高版本php不兼容,改用下面的 $postStr = file_get_contents("php://input"); //extract post data if(!empty($postStr)){ //解析post来的XML为一个对象$postObj $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; //请求消息的用户 $toUsername = $postObj->ToUserName; //"我"的公众号id $keyword = trim($postObj->Content); //消息内容 $time = time(); //时间戳 $msgtype = 'text'; //消息类型:文本 $textTpl = ""; if($postObj->MsgType == 'event'){ //如果XML信息里消息类型为event if($postObj->Event == 'subscribe'){ //如果是订阅事件 $contentStr = "欢迎订阅柔晶美!\n更多精彩内容:https://blog.dzbfsj.com"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); } } //分割字符为数组,比如1/张三 $sz = explode("/",$keyword); if(count($sz)>1){ $sn = $sz[0] - 1;//序号 $bm = $datas['data'][$sn];//表名 $tiaojian = $datas['tiaojian'][$sn];//查询条件 $shuru = $sz[1];//用户输入查询的内容 //开始查询数据库 $cxjgs = array(); $cx = "select * from `$bm` where `$tiaojian` = '$shuru'"; //SELECT * FROM `初二8班语文期末质量检测一` WHERE `姓名` = '张书源' $sql = iconv("utf-8","gbk",$cx); $cxquery = mysqli_query($conn,$sql); while ($row = mysqli_fetch_array($cxquery, MYSQLI_ASSOC)){ $cxjgs[] = $row; } if(count($cxjgs)>0){ $zhbm = mult_iconv('gbk','utf-8',$cxjgs); $i = 0; foreach ($zhbm as $value){ $i++; $txt = '【查询结果'.$i."】\n"; foreach ($value as $key=>$val){ $txt = $txt.$key.':'.$val."\n"; } } $contentStr = $txt; }else{ $contentStr = '没有查询到结果!'; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); }else{ //输入任意字符,回复查询帮助 $contentStr = $datas['list']; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); } }else { echo ""; exit; } } %s
实现效果,用户随意输入文字,回复查询方法;用户根据查询方法输入文字,回复查询结果。