一、简介
新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,默认代码中没有对这一事件进行相应回复处理。
在新用户关注公众平台后,可能想知道该平台提供了哪些功能,以及怎样使用该平台,通俗一点讲就是该平台的“使用说明书”。
本文将详细讲述对subscribe事件的处理过程,回复相应信息提升交互性。
二、思路分析
微信目前提供了五种消息类型,分别为:
- 文本消息(text);
- 图片消息(image);
- 地理位置消息(location);
- 链接消息(link);
- 事件推送(event);
接收到消息后,首先需要对消息类型做出判断,然后再针对不同类型的消息做出处理。在事件推送中,事件类型又分为三种,subscribe(订阅)、unsubscribe(取消订阅)、click(自定义菜单点击事件),还需要再加一次判断;判断为subscribe事件后,根据设定好的欢迎消息,回复给用户。
三、判断消息类型
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$rx_type = trim($postobj->msgtype);
switch($rx_type)
{
case "text":
$resultstr = $this->handletext($postobj);
break;
case "event":
$resultstr = $this->handleevent($postobj);
break;
default:
$resultstr = "unknow msg type: ".$rx_type;
break;
}
说明:
$rx_type = trim($postobj->msgtype);
得到消息类型;
case "text":
$resultstr = $this->handletext($postobj);
使用handletext() 函数处理文本消息;
case "event":
$resultstr = $this->handleevent($postobj);
使用handleevent() 函数处理事件推送;
四、判断事件类型
switch ($object->event)
{
case "subscribe":
$contentstr = "感谢您关注【卓锦苏州】"."n"."微信号:zhuojinsz"."n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."n"."目前平台功能如下:"."n"."【1】 查天气,如输入:苏州天气"."n"."【2】 查公交,如输入:苏州公交178"."n"."【3】 翻译,如输入:翻译i love you"."n"."【4】 苏州信息查询,如输入:苏州观前街"."n"."更多内容,敬请期待...";
break;
default :
$contentstr = "unknow event: ".$object->event;
break;
}
说明:
如果是subscribe事件,设定回复内容为“感谢您关注【卓锦苏州】...”;
五、完整代码
<?php
/**
* wechat php test
*/
//define your token
define("token", "zhuojin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->responsemsg();
//$wechatobj->valid();
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)){
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$rx_type = trim($postobj->msgtype);
switch($rx_type)
{
case "text":
$resultstr = $this->handletext($postobj);
break;
case "event":
$resultstr = $this->handleevent($postobj);
break;
default:
$resultstr = "unknow msg type: ".$rx_type;
break;
}
echo $resultstr;
}else {
echo "";
exit;
}
}
public function handletext($postobj)
{
$fromusername = $postobj->fromusername;
$tousername = $postobj->tousername;
$keyword = trim($postobj->content);
$time = time();
$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>";
if(!empty( $keyword ))
{
$msgtype = "text";
$contentstr = "welcome to wechat world!";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
echo $resultstr;
}else{
echo "input something...";
}
}
public function handleevent($object)
{
$contentstr = "";
switch ($object->event)
{
case "subscribe":
$contentstr = "感谢您关注【卓锦苏州】"."n"."微信号:zhuojinsz"."n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."n"."目前平台功能如下:"."n"."【1】 查天气,如输入:苏州天气"."n"."【2】 查公交,如输入:苏州公交178"."n"."【3】 翻译,如输入:翻译i love you"."n"."【4】 苏州信息查询,如输入:苏州观前街"."n"."更多内容,敬请期待...";
break;
default :
$contentstr = "unknow event: ".$object->event;
break;
}
$resultstr = $this->responsetext($object, $contentstr);
return $resultstr;
}
public function responsetext($object, $content, $flag=0)
{
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[%s]]></content>
<funcflag>%d</funcflag>
</xml>";
$resultstr = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content, $flag);
return $resultstr;
}
private function checksignature()
{
$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 ){
return true;
}else{
return false;
}
}
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!