一、hello2bizuser旧事件的处理
在老的关注事件中,用户关注微信公众平台账号后,系统会帮用户发送一条hello2bizuser的文本给公众账号,公众账号后台开发模式下通过判断hello2bizuser这个词来实现发送欢迎词。
代码样例如下:
if ($keyword == "hello2bizuser"){
$contentstr = "";
$resultstr = $this->transmittext($object, $contentstr, $funcflag);
return $resultstr;
}
做出基础接口上的修改,影响会是所有人。一般不会轻易做这样的改动。
为什么微信要修改这一事件了,这个方法的弊端在于,如果用户没有判断这一事件,那么就不会有欢迎词,本来这也没什么关系,不出现欢迎词也不影响什么。但很多人的程序代码里面,所有流程直接就是判断关键词。比如我们曾看到过一家医院的微信账号,用户发送挂号的号码,就显示前面有多少人排列,但后台程序没有做区分,把hello2bizuser也当成挂号单发送过去,返回没有找到hello2bizuser这一挂号,不知道前面有多少人,搞得用户莫名奇妙。还有就是用户如果主动发送一个hello2bizuser过去,那么得到的也是和欢迎词一样的内容,虽然很少有用户会去发这个东西。
另一方面,将用户关注做成事件,更有利于统计功能的实现。利用这个事件,我们就更能容易的判断出关注人数和退订人数,而原来的hello2bizuser文本推送判断是有可能不准确的,因为用户可以手工发送,形成虚假关注统计。
二、“subscribe”订阅事件判断
subscribe是一种新的事件,我们先需要做事件类型做判断,我们在官方样例中增加对这一事件的判断,修改如下:
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$rx_type = trim($postobj->msgtype);
switch ($rx_type)
{
case "text":
$resultstr = $this->receivetext($postobj);
break;
case "event":
$resultstr = $this->receiveevent($postobj);
break;
default:
$resultstr = "unknow msg type: ".$rx_type;
break;
}
然后在事件接收处理函数中再判断订阅事件 :
private function receiveevent($object)
{
$contentstr = "";
switch ($object->event)
{
case "subscribe":
$contentstr = "您好,欢迎关注方倍工作室。新感觉,新体验!";
break;
}
$resultstr = $this->transmittext($object, $contentstr);
return $resultstr;
}
这样就完成了“subscribe”的订阅事件下的处理。
二、完整代码
<?php
define("token", "方倍工作室");
$wechatobj = new wechatcallbackapitest();
$wechatobj->responsemsg();
class wechatcallbackapitest
{
public function responsemsg()
{
$poststr = $globals["http_raw_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->receivetext($postobj);
break;
case "event":
$resultstr = $this->receiveevent($postobj);
break;
default:
$resultstr = "unknow msg type: ".$rx_type;
break;
}
echo $resultstr;
}else {
echo "";
exit;
}
}
private function receivetext($object)
{
$funcflag = 0;
$keyword = trim($object->content);
$resultstr = "";
$cityarray = array();
$contentstr = "";
$needarray = false;
$illegal = false;
$saytome = false;
if ($keyword == "hello2bizuser"){
$contentstr = "欢迎关注方倍工作室,这其实是老的欢迎词,你关注时收不到了";
$resultstr = $this->transmittext($object, $contentstr, $funcflag);
return $resultstr;
}else {
}
}
private function receiveevent($object)
{
$contentstr = "";
switch ($object->event)
{
case "subscribe":
$contentstr = "您好,欢迎关注方倍工作室。新感觉,新体验!";
break;
}
$resultstr = $this->transmittext($object, $contentstr);
return $resultstr;
}
private function transmittext($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;
}
}
?>
更多微信公众平台消息接口开发从hello2bizuser文本到subscribe事件 。