微信开发之消息回复
来源:互联网 发布:java xpath解析html 编辑:程序博客网 时间:2024/06/04 17:50
a、纯文本回复
上代码:
public function index() { //将timestamp,nonce,token按字典序排序 $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = 'weixin'; $signature = $_GET['signature']; //将排序之后的三个参数拼接之后用sha1加密 $array = array($timestamp, $nonce, $token); sort($array); $tmpstr = implode('', $array); $tmpstr = sha1($tmpstr); //将加密后的字符串与sianature进行对比,判断请求是否来自于微信 if ($tmpstr == $signature && $_GET['echostr']) {//第一次接入微信Api echo $_GET['echostr']; exit; } else { $this->responseMsg(); } } public function responseMsg() { //接收到的用户消息格式<xml> $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];// 接收到的消息格式// <xml>// <ToUserName><![CDATA[toUser]]></ToUserName>// <FromUserName><![CDATA[FromUser]]></FromUserName>// <CreateTime>123456789</CreateTime>// <MsgType><![CDATA[event]]></MsgType>// <Event><![CDATA[subscribe]]></Event>// </xml> //把接收到的xml格式数据包转化为对象格式 $postObj = simplexml_load_string($postArr);// 被动回复用户消息格式// <xml>// <ToUserName><![CDATA[toUser]]></ToUserName>// <FromUserName><![CDATA[fromUser]]></FromUserName>// <CreateTime>12345678</CreateTime>// <MsgType><![CDATA[text]]></MsgType>// <Content><![CDATA[你好]]></Content>// </xml> if (strtolower($postObj->MsgType) == 'event') {//判断是否为订阅的事件推送 if (strtolower($postObj->Event) == 'subscribe') {//判断是否为关注的事件推送 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $content = '你好:'.$postObj->FromUserName.'!欢迎关注我们的微信公众号:'.$postObj->ToUserName;; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; } }elseif(strtolower($postObj->MsgType == 'text')){ if($postObj->Content == 'hello'){ $content = '我爱你!'; }elseif($postObj->Content == 'baidu'){ $content = '<a href="http://www.baidu.com"> 百度</a>'; } $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template,$toUser,$fromUser,$time,$msgType,$content); echo $info; } } }
@$ b、单图文回复与多图文::区别,foreach个数
$toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'news'; $arrdata = [ [ 'Title' => '端午节促销', 'Description' => '庆祝端午节,只要99只要99,就这一天', 'PicUrl' => 'http://hiphotos.baidu.com/zhixin/abpic/item/d1571724ab18972bbb8d648ce4cd7b899f510a85.jpg', 'Url' => 'http://www.baidu.com' ], [ 'Title' => '元宵节促销', 'Description' => '庆祝元宵节,只要9.9只要9.9,就这一天', 'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png', 'Url' => 'http://www.mnxiao.top' ], [ 'Title' => '元宵节促销', 'Description' => '庆祝元宵节,只要9.9只要9.9,就这一天', 'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png', 'Url' => 'http://www.mnxiao.top' ], ]; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>" . count($arrdata) . "</ArticleCount> <Articles>"; foreach ($arrdata as $value) { $template .= "<item> <Title><![CDATA[" . $value['Title'] . "]]></Title> <Description><![CDATA[" . $value['Description'] . "]]></Description> <PicUrl><![CDATA[" . $value['PicUrl'] . "]]></PicUrl> <Url><![CDATA[" . $value['Url'] . "]]></Url> </item> "; } $template .= "</Articles> </xml>"; echo sprintf($template, $toUser, $fromUser, $time, $msgType); }
1 0
- 微信开发之消息回复
- C#开发微信公众号之消息自动回复
- 微信开发之自动回复图文消息
- 微信公众号开发之文本消息自动回复
- 【微信公众号开发】自我学习第四章:消息回复之翻译回复
- PHP微信开发---模板消息回复
- [微信开发五]--图文消息回复
- 微信消息自动回复开发
- 微信开发 (一) 消息回复
- 微信开发基本消息回复
- php 微信开发回复消息
- 【微信公众号开发】自我学习第三章:消息回复之文本回复和图文回复
- 微信消息回复
- 微信公众平台开发【发送消息】被动回复消息
- 微信开发_接收消息,回复消息(3)
- 微信开发中的消息验证与消息回复
- 微信消息管理之被动回复用户消息
- 微信消息管理之被动回复用户消息
- 【安卓开源项目收藏】
- 服务端与客户端交互
- 决策树与随机森林
- iOS 零耦合架构代码生成器
- 一步一步学习 ReactNative + Redux(1)
- 微信开发之消息回复
- 图解Git相关工作流
- 机器学习
- 正式项目中的一些编码规范
- Ajax的操作
- ORACLE DATABASE 11G RAC 知识图文详细教程之缓存融合技术和主要后台进程(四)
- 搜索引擎和知识图谱那些事 (上).基础篇
- 【matlab】export_fig 去掉白边、保存图像
- js函数解释(包括内嵌,对象等)