微信公众号开发之消息自动回复

1.PHP示例代码下载

下载地址1:http://pan.baidu.com/s/1nvlhbnV

下载地址2:https://mp.weixin.qq.com/wiki/home/index.html(开始开发-》接入指南-》PHP示例代码下载)

2.wx_sample.php初始代码

 1 <?php
 2 /**
 3   * wechat php test
 4   */
 5
 6 //define your token
 7 define("TOKEN", "weixin");
 8 $wechatObj = new wechatCallbackapiTest();
 9 $wechatObj->valid();
10
11 class wechatCallbackapiTest
12 {
13     public function valid()
14     {
15         $echoStr = $_GET["echostr"];
16
17         //valid signature , option
18         if($this->checkSignature()){
19             echo $echoStr;
20             exit;
21         }
22     }
23
24     public function responseMsg()
25     {
26         //get post data, May be due to the different environments
27         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
28
29           //extract post data
30         if (!empty($postStr)){
31                 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
32                    the best way is to check the validity of xml by yourself */
33                 libxml_disable_entity_loader(true);
34                   $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
35                 $fromUsername = $postObj->FromUserName;
36                 $toUsername = $postObj->ToUserName;
37                 $keyword = trim($postObj->Content);
38                 $time = time();
39                 $textTpl = "<xml>
40                             <ToUserName><![CDATA[%s]]></ToUserName>
41                             <FromUserName><![CDATA[%s]]></FromUserName>
42                             <CreateTime>%s</CreateTime>
43                             <MsgType><![CDATA[%s]]></MsgType>
44                             <Content><![CDATA[%s]]></Content>
45                             <FuncFlag>0</FuncFlag>
46                             </xml>";
47                 if(!empty( $keyword ))
48                 {
49                       $msgType = "text";
50                     $contentStr = "Welcome to wechat world!";
51                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
52                     echo $resultStr;
53                 }else{
54                     echo "Input something...";
55                 }
56
57         }else {
58             echo "";
59             exit;
60         }
61     }
62
63     private function checkSignature()
64     {
65         // you must define TOKEN by yourself
66         if (!defined("TOKEN")) {
67             throw new Exception(‘TOKEN is not defined!‘);
68         }
69
70         $signature = $_GET["signature"];
71         $timestamp = $_GET["timestamp"];
72         $nonce = $_GET["nonce"];
73
74         $token = TOKEN;
75         $tmpArr = array($token, $timestamp, $nonce);
76         // use SORT_STRING rule
77         sort($tmpArr, SORT_STRING);
78         $tmpStr = implode( $tmpArr );
79         $tmpStr = sha1( $tmpStr );
80
81         if( $tmpStr == $signature ){
82             return true;
83         }else{
84             return false;
85         }
86     }
87 }
88
89 ?>

wx_sample.php

3.调用回复信息方法

在wx_sample.php文件中注释掉$wechatObj->valid();,在其下增加一句“$wechatObj->responseMsg();”。

 1 <?php
 2 /**
 3   * wechat php test
 4   */
 5
 6 //define your token
 7 define("TOKEN", "weixin");
 8 $wechatObj = new wechatCallbackapiTest();
 9 //$wechatObj->valid();//接口验证
10 $wechatObj->responseMsg();//调用回复消息方法
11 class wechatCallbackapiTest
12 {
13     public function valid()
14     {
15         $echoStr = $_GET["echostr"];
16
17         //valid signature , option
18         if($this->checkSignature()){
19             echo $echoStr;
20             exit;
21         }
22     }
23
24     public function responseMsg()
25     {
26         //get post data, May be due to the different environments
27         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
28
29           //extract post data
30         if (!empty($postStr)){
31                 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
32                    the best way is to check the validity of xml by yourself */
33                 libxml_disable_entity_loader(true);
34                   $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
35                 $fromUsername = $postObj->FromUserName;
36                 $toUsername = $postObj->ToUserName;
37                 $keyword = trim($postObj->Content);
38                 $time = time();
39                 $textTpl = "<xml>
40                             <ToUserName><![CDATA[%s]]></ToUserName>
41                             <FromUserName><![CDATA[%s]]></FromUserName>
42                             <CreateTime>%s</CreateTime>
43                             <MsgType><![CDATA[%s]]></MsgType>
44                             <Content><![CDATA[%s]]></Content>
45                             <FuncFlag>0</FuncFlag>
46                             </xml>";
47                 if(!empty( $keyword ))
48                 {
49                       $msgType = "text";
50                     $contentStr = "Welcome to wechat world!";
51                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
52                     echo $resultStr;
53                 }else{
54                     echo "Input something...";
55                 }
56
57         }else {
58             echo "";
59             exit;
60         }
61     }
62
63     private function checkSignature()
64     {
65         // you must define TOKEN by yourself
66         if (!defined("TOKEN")) {
67             throw new Exception(‘TOKEN is not defined!‘);
68         }
69
70         $signature = $_GET["signature"];
71         $timestamp = $_GET["timestamp"];
72         $nonce = $_GET["nonce"];
73
74         $token = TOKEN;
75         $tmpArr = array($token, $timestamp, $nonce);
76         // use SORT_STRING rule
77         sort($tmpArr, SORT_STRING);
78         $tmpStr = implode( $tmpArr );
79         $tmpStr = sha1( $tmpStr );
80
81         if( $tmpStr == $signature ){
82             return true;
83         }else{
84             return false;
85         }
86     }
87 }
88
89 ?>

调用回复信息方法

4.关键词自动回复和关注回复

$keyword保存着用户微信端发来的文本信息。

官方开发者文档:https://mp.weixin.qq.com/wiki/home/index.html(消息管理-》接收消息-接收事件推送-》1.关注/取消关注事件)

关注事件与一般的文本消息有两处不同,一是MsgType值是event,二是增加了Event值是subscribe。由于官方文档(最初的wx_sample.php)没有提取这个参数,需要我们自己提取。在程序中增加两个变量$msgType和$event。

  1 <?php
  2 /**
  3   * wechat php test
  4   */
  5
  6 //define your token
  7 define("TOKEN", "weixin");
  8 $wechatObj = new wechatCallbackapiTest();
  9 //$wechatObj->valid();//接口验证
 10 $wechatObj->responseMsg();//调用回复消息方法
 11 class wechatCallbackapiTest
 12 {
 13     public function valid()
 14     {
 15         $echoStr = $_GET["echostr"];
 16
 17         //valid signature , option
 18         if($this->checkSignature()){
 19             echo $echoStr;
 20             exit;
 21         }
 22     }
 23
 24     public function responseMsg()
 25     {
 26         //get post data, May be due to the different environments
 27         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 28
 29           //extract post data
 30         if (!empty($postStr)){
 31                 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
 32                    the best way is to check the validity of xml by yourself */
 33                 libxml_disable_entity_loader(true);
 34                   $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
 35                 $fromUsername = $postObj->FromUserName;
 36                 $toUsername = $postObj->ToUserName;
 37                 $keyword = trim($postObj->Content);
 38                 $time = time();
 39                 $msgType = $postObj->MsgType;//消息类型
 40                 $event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
 41                 $textTpl = "<xml>
 42                             <ToUserName><![CDATA[%s]]></ToUserName>
 43                             <FromUserName><![CDATA[%s]]></FromUserName>
 44                             <CreateTime>%s</CreateTime>
 45                             <MsgType><![CDATA[%s]]></MsgType>
 46                             <Content><![CDATA[%s]]></Content>
 47                             <FuncFlag>0</FuncFlag>
 48                             </xml>";
 49
 50                 switch($msgType){
 51                     case "event":
 52                     if($event=="subscribe"){
 53                         $contentStr = "Hi,欢迎关注海仙日用百货!"."\n"."回复数字‘1‘,了解店铺地址."."\n"."回复数字‘2‘,了解商品种类.";
 54                     }
 55                     break;
 56                     case "text":
 57                         switch($keyword){
 58                             case "1":
 59                             $contentStr = "店铺地址:"."\n"."杭州市江干艮山西路233号新东升市场地下室第一排.";
 60                             break;
 61                             case "2":
 62                             $contentStr = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"
 63                                          ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等.";
 64                             break;
 65                             default:
 66                             $contentStr = "对不起,你的内容我会稍后回复";
 67                         }
 68                     break;
 69                 }
 70                 $msgType = "text";
 71                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 72                 echo $resultStr;
 73         }else {
 74             echo "";
 75             exit;
 76         }
 77     }
 78
 79     private function checkSignature()
 80     {
 81         // you must define TOKEN by yourself
 82         if (!defined("TOKEN")) {
 83             throw new Exception(‘TOKEN is not defined!‘);
 84         }
 85
 86         $signature = $_GET["signature"];
 87         $timestamp = $_GET["timestamp"];
 88         $nonce = $_GET["nonce"];
 89
 90         $token = TOKEN;
 91         $tmpArr = array($token, $timestamp, $nonce);
 92         // use SORT_STRING rule
 93         sort($tmpArr, SORT_STRING);
 94         $tmpStr = implode( $tmpArr );
 95         $tmpStr = sha1( $tmpStr );
 96
 97         if( $tmpStr == $signature ){
 98             return true;
 99         }else{
100             return false;
101         }
102     }
103 }
104
105
106 ?>

关键词回复和关注回复

时间: 2024-08-05 02:40:35

微信公众号开发之消息自动回复的相关文章

[.NET] 简单接入微信公众号开发:实现自动回复

简单接入微信公众号开发:实现自动回复 一.前提 先申请微信公众号的授权,找到或配置几个关键的信息(开发者ID.开发者密码.IP白名单.令牌和消息加解密密钥等). 二.基本配置信息解读 开发者ID:固定的: 开发者密码:自己扫一下就可以看到: IP白名单:设置自己配置服务器的地址: 服务器地址(URL):稍后详解: 令牌:随便写,按规则: 消息加解密密钥:随便写,或者随机生成: 三.配置服务器地址(URL) 服务器地址(URL)应该怎么配置呢?图片上的配置的地址是:http://www.nidie

.net微信公众号开发——群发消息

作者:王先荣    本文将介绍微信公众号开发中用于群发消息的类MassMessage,包括:(1)MassMessage类:(2)群发:(3)删除:(4)预览:(5)查询发送状态:(6)接收推送群发结果事件.    源代码地址:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/tree/master/PublicAccount/MassMessage    演示地址:http://www.xrwang.net/Example/M

.net微信公众号开发——模板消息

本文介绍微信公众号中的模板消息,包括以下内容:(1)TemplateMessage类简介:(2)设置所属行业:(3)获得模板id:(4)发送模板消息:(5)接收推送模板消息发送结果事件.    本文演示地址:http://xrwang.net/Example/TemplateMessage.aspx    本文源代码地址:    http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/tree/master/PublicAccount

微信公众号开发之消息的接收与被动回复消息

原文:http://blog.csdn.net/qczxl/article/details/51580946 微信官方文档 接收普通消息(http://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html) 接收事件推送(http://mp.weixin.qq.com/wiki/7/9f89d962eba4c5924ed95b513ba69d9b.html) 发送被动回复消息(http://mp.weixin.qq.com/

C#微信公众号开发系列教程三(消息体签名及加解密)

  C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南)    距离上一篇博文已经半个月了,本来打算每两天更新一次的,但可怜苦逼码农无日无夜的加班.第一篇博文发表后,博文视点的编辑就找到我,问我想不想出版这个系列,我当时瞬间就想到了王大锤的独白,想想真的是有点小激动,后面按照那边的要求,提交了申请书,也提交了目录,可惜文笔不行,再加上最近太忙,样稿一直没有给他,感觉挺愧疚了.真心希望能帮一下迷茫的

C#微信公众号开发系列教程五(接收事件推送与消息排重)

微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C#微信公众号开发系列教程四(接收普通消息) C#微信公众号开发系列教程五(接收事件推送与消息排重) 在上一篇的博文中讲到,微信的消息可以大体分为两种类型,一种是包括:文本,语音,图片等的普通消息,另一种就是本篇要将的事件类型.包括:关注/取消关注事件,扫描带参数二维码事件,上报地理位置事件,自定义菜

微信公众号开发系列-发送客服消息

下面是做微信公众号开发用到最多的两个客服消息发送类型,文本信息和图文信息. 1.发送文本消息 { "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" } } 參数 是否必须 说明 access_token 是 调用接口凭证 touser 是 普通用户openid msgtype

微信公众号开发之自动消息回复和自定义菜单

(一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 上一篇我们大致讲解了下微信公众号开发的基本原理和流程概述.本章主要是对文本消息回复和自定义菜单做一个记录和分解 消息回复 处理请求,并响应 1)关注 也可参考官网文档:https://mp.weixin.qq.com/wiki 当微信用户关注公众账号时,可以给其适当的提示.可以是欢迎词,可以是帮助提示.示例代码如下: class EventHandler : IHandler

.NET微信公众号开发-6.0模板消息

一.前言 为了保证用户不受到骚扰,在开发者出现需要主动提醒.通知用户时,才允许开发者在公众平台网站中模板消息库中选择模板,选择后获得模板ID,再根据模板ID向用户主动推送提醒.通知消息.这个意思也就是,我们作为微信商户,不能主动的给用户推送消息,如果这个功能完全开放,那么用户有可能会受到大量的垃圾信息,为了做一个很好的控制,微信那边给我们开放了一个模板消息,通过模板消息我们可以友好的给用户发送一些相关的消息提醒. 二.开发前的准备 1.0模板消息官方文档地址 2.0查看我们的模板消息是否申请成功