微信公众号开发消息自动回复服务器端php代码
作者:xlnxin发布时间:2022-10-02分类:其他教程浏览:1250
导读:关注事件与一般的文本消息有两处不同,一是msgtype值是event,二是增加了event值是subscribe。由于官方文档(最初的wx_sample.php)没有提取这个参数,...
关注事件与一般的文本消息有两处不同,一是msgtype值是event,二是增加了event值是subscribe。由于官方文档(最初的wx_sample.php)没有提取这个参数,需要我们自己提取。在程序中增加两个变量$msgtype和$event。
<?php/** * wechat php test *///define your tokendefine("token", "weixin");$wechatobj = new wechatcallbackapitest();//$wechatobj->valid();//接口验证$wechatobj->responsemsg();//调用回复消息方法class wechatcallbackapitest{ public function valid() { $echostr = $_get["echostr"]; //valid signature , option if($this->checksignature()){ echo $echostr; exit; } } public function responsemsg() { //get post data, may be due to the different environments $poststr = $globals["http_raw_post_data"]; //extract post data if (!empty($poststr)){ /* libxml_disable_entity_loader is to prevent xml external entity injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $fromusername = $postobj->fromusername; $tousername = $postobj->tousername; $keyword = trim($postobj->content); $time = time(); $msgtype = $postobj->msgtype;//消息类型 $event = $postobj->event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅) $texttpl = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <content><![cdata[%s]]></content> <funcflag>0</funcflag> </xml>"; switch($msgtype){ case "event": if($event=="subscribe"){ $contentstr = "hi,欢迎关注海仙日用百货!"."\n"."回复数字'1',了解店铺地址."."\n"."回复数字'2',了解商品种类."; } break; case "text": switch($keyword){ case "1": $contentstr = "店铺地址:"."\n"."杭州市江干艮山西路233号新东升市场地下室第一排."; break; case "2": $contentstr = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、" ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等."; break; default: $contentstr = "对不起,你的内容我会稍后回复"; } break; } $msgtype = "text"; $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); echo $resultstr; }else { echo ""; exit; } } private function checksignature() { // you must define token by yourself if (!defined("token")) { throw new exception('token is not defined!'); } $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $token = token; $tmparr = array($token, $timestamp, $nonce); // use sort_string rule sort($tmparr, sort_string); $tmpstr = implode( $tmparr ); $tmpstr = sha1( $tmpstr ); if( $tmpstr == $signature ){ return true; }else{ return false; } }}?>相关推荐
- 快来制作个性表情包pk吧
- 宝塔面板phpMyAdmin提示:服务器和客户端上指示的HTTPS之间不匹配
- 微信小程序button如何去掉边框设置透明
- 微信小程序input组件解决iphone拼音字符maxlength长度问题
- 微信小程序弹出对话框输入名字
- 微信小程序页面跳转:wx.switchTab、wx.reLaunch、wx.redirectTo、wx.navigateTo、wx.navigateBack”的使用区别介绍
- 小程序默认icon
- 微信小程序image组件中aspectFill和widthfix、scaleToFill对比
- 鸿蒙电脑如何安装打印机驱动
- excel2007宏功能灰色怎么办
- 其他教程排行
-
- 1抖音极速版给大家发37元过年红包啦,快来领取吧!
- 2统信uos系统默认root密码是多少
- 3admin、0000、admin888、123456 md5加密后16位和32位代码
- 4国产安可立思辰打印机手动双面打印设置教程
- 5Windows10 DVDRW识别为CD驱动解决方法,windows无法读取驱动器E:\中的光盘解决方法
- 6navicat premium 15破解版64位32位 v15.0.3激活方法
- 7小程序Cannot read property 'enableUpdateWxAppCode'解决方法
- 8快手极速版给大家发56元新年红包啦,快来领取吧!
- 9查找对话框跑出屏幕外看不见了,怎么拖回来?
- 最近发表

