微信中新用户关注我们后,及时反馈给用户信息是一个非常友好的功能,查看微信开发者文档并没找到相关说明。
新用户订阅,将由之前推送一条“Hello2BizUser”文本信息,
if ($keyword == "Hello2BizUser"){//新用户关注 $contentStr = "你好,欢迎您.."; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
微信公众平台由于接口升级后,新用户订阅,将由之前推送一条“Hello2BizUser”文本,变化为推送一条“subscribe”的事件。网上找到了一篇详细讲解。
一、Hello2BizUser旧事件的处理
在老的关注事件中,用户关注微信公众平台账号后,系统会帮用户发送一条Hello2BizUser的文本给公众账号,公众账号后台开发模式下通过判断Hello2BizUser这个词来实现发送欢迎词。代码样例如下:
if ($keyword == "Hello2BizUser"){ $contentStr = "欢迎您,这是老的欢迎词,现在已经失效了"; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
将用户关注做成事件,更有利于统计功能的实现。利用这个事件,我们就更能容易的判断出关注人数和退订人数,而原来的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”的订阅事件下的处理。
二、修改后完整代码
define("TOKEN", "XXX"); $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; } }
感谢作者提供完整的代码
转载请指明文章出处:https://9iphp.com/web/php/397.html