一、简介
新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,默认代码中没有对这一事件进行相应回复处理。
在新用户关注公众平台后,可能想知道该平台提供了哪些功能,以及怎样使用该平台,通俗一点讲就是该平台的“使用说明书”。
本文将详细讲述对subscribe事件的处理过程,回复相应信息提升交互性。
二、思路分析
微信目前提供了五种消息类型,分别为:
文本消息(text);
图片消息(image);
地理位置消息(location);
链接消息(link);
事件推送(event);
接收到消息后,首先需要对消息类型做出判断,然后再针对不同类型的消息做出处理。在事件推送中,事件类型又分为三种,subscribe(订阅)、unsubscribe(取消订阅)、CLICK(自定义菜单点击事件),还需要再加一次判断;判断为subscribe事件后,根据设定好的欢迎消息,回复给用户。
三、判断消息类型
view sourceprint?01.$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
02.$RX_TYPE = trim($postObj->MsgType);
03.
04.switch($RX_TYPE)
05.{
06.case "text":
07.$resultStr = $this->handleText($postObj);
08.break;
09.case "event":
10.$resultStr = $this->handleEvent($postObj);
11.break;
12.default:
13.$resultStr = "Unknow msg type: ".$RX_TYPE;
14.break;
15.}
说明:
view sourceprint?1.$RX_TYPE = trim($postObj->MsgType); 得到消息类型;
2.
3.case "text":
4. $resultStr = $this->handleText($postObj); 使用handleText() 函数处理文本消息;
5.
6.case "event":
7. $resultStr = $this->handleEvent($postObj); 使用handleEvent() 函数处理事件推送;
四、判断事件类型
view sourceprint?1.
view sourceprint?01.switch ($object->Event)
02.{
03.case "subscribe":
04.$contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
05.break;
06.default :
07.$contentStr = "Unknow Event: ".$object->Event;
08.break;
09.}
五、完整代码
view sourceprint?001.<!--?php
002./**
003.* wechat php test
004.*/
005.
006.//define your token
007.define("TOKEN", "zhuojin");
008.$wechatObj = new wechatCallbackapiTest();
009.$wechatObj--->responseMsg();
010.//$wechatObj->valid();
011.
012.class wechatCallbackapiTest
013.{
014./*public function valid()
015.{
016.$echoStr = $_GET["echostr"];
017.
018.//valid signature , option
019.if($this->checkSignature()){
020.echo $echoStr;
021.exit;
022.}
023.}*/
024.
025.public function responseMsg()
026.{
027.//get post data, May be due to the different environments
028.$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
029.
030.//extract post data
031.if (!emptyempty($postStr)){
032.
033.$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
034.$RX_TYPE = trim($postObj->MsgType);
035.
036.switch($RX_TYPE)
037.{
038.case "text":
039.$resultStr = $this->handleText($postObj);
040.break;
041.case "event":
042.$resultStr = $this->handleEvent($postObj);
043.break;
044.default:
045.$resultStr = "Unknow msg type: ".$RX_TYPE;
046.break;
047.}
048.echo $resultStr;
049.}else {
050.echo "";
051.exit;
052.}
053.}
054.
055.public function handleText($postObj)
056.{
057.$fromUsername = $postObj->FromUserName;
058.$toUsername = $postObj->ToUserName;
059.$keyword = trim($postObj->Content);
060.$time = time();
061.$textTpl = "<xml>
062.<tousername><!--[CDATA[%s]]--></tousername>
063.<fromusername><!--[CDATA[%s]]--></fromusername>
064.<createtime>%s</createtime>
065.<msgtype><!--[CDATA[%s]]--></msgtype>
066.<content><!--[CDATA[%s]]--></content>
067.<funcflag>0</funcflag>
068.</xml>";
069.if(!emptyempty( $keyword ))
070.{
071.$msgType = "text";
072.$contentStr = "Welcome to wechat world!";
073.$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$msgType, $contentStr);
074.echo $resultStr;
075.}else{
076.echo "Input something...";
077.}
078.}
079.
080.public function handleEvent($object)
081.{
082.$contentStr = "";
083.switch ($object->Event)
084.{
085.case "subscribe":
086.$contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
087.break;
088.default :
089.$contentStr = "Unknow Event: ".$object->Event;
090.break;
091.}
092.$resultStr = $this->responseText($object, $contentStr);
093.return $resultStr;
094.}
095.
096.public function responseText($object, $content, $flag=0)
097.{
098.$textTpl = "<xml>
099.<tousername><!--[CDATA[%s]]--></tousername>
100.<fromusername><!--[CDATA[%s]]--></fromusername>
101.<createtime>%s</createtime>
102.<msgtype><!--[CDATA[text]]--></msgtype>
103.<content><!--[CDATA[%s]]--></content>
104.<funcflag>%d</funcflag>
105.</xml>";
106.$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
107.return $resultStr;
108.}
109.
110.private function checkSignature()
111.{
112.$signature = $_GET["signature"];
113.$timestamp = $_GET["timestamp"];
114.$nonce = $_GET["nonce"];
115.
116.$token = TOKEN;
117.$tmpArr = array($token, $timestamp, $nonce);
118.sort($tmpArr);
119.$tmpStr = implode( $tmpArr );
120.$tmpStr = sha1( $tmpStr );
121.
122.if( $tmpStr == $signature ){
123.return true;
124.}else{
125.return false;
126.}
127.}
128.}
129.
130.?>