1. Processing of the old Hello2BizUser event
In the old attention event, after the user follows the WeChat public platform account, the system will help the user send a Hello2BizUser text to the public account. In the background development mode of the public account, the welcome message is sent by judging the word Hello2BizUser.
The code sample is as follows:
if ($keyword == "Hello2BizUser"){ $contentStr = "PHP中文网"; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
Making changes to the basic interface will affect everyone. Generally, such changes will not be made easily.
Why does WeChat need to modify this event? The disadvantage of this method is that if the user does not judge this event, then there will be no welcome message. Originally, this does not matter. The absence of the welcome message will not affect it. What. But in many people's program codes, all processes are directly based on judging keywords. For example, we have seen a hospital's WeChat account. When the user sends the registration number, it will display how many people are lined up in front of it. However, the background program does not make a distinction and sends Hello2BizUser as a registration order. The registration number Hello2BizUser is not found. I don’t know how many people are in front of me, which makes users confused. Also, if the user takes the initiative to send a Hello2BizUser, they will get the same content as the welcome message, although few users will send this thing.
On the other hand, turning user attention into events is more conducive to the realization of statistical functions. Using this event, we can more easily determine the number of followers and unsubscribers. However, the original Hello2BizUser text push determination may be inaccurate because users can send it manually, forming false follow statistics.
2. "subscribe" subscription event judgment
subscribe is a new event. We first need to judge the event type. We Add the judgment of this event in the official sample and modify it as follows:
$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; }
Then judge the subscription event in the event receiving processing function:
private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "您好,欢迎关注方倍工作室。新感觉,新体验!"; break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; }
This completes the processing of the "subscribe" subscription event.
2. Complete code
<?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; } } ?>
More WeChat public platform message interface development from Hello2BizUser text to subscribe event. For related articles, please pay attention to the PHP Chinese website!