Home WeChat Applet WeChat Development WeChat public platform message interface development from Hello2BizUser text to subscribe event

WeChat public platform message interface development from Hello2BizUser text to subscribe event

Mar 04, 2017 pm 02:49 PM
Micro-channel public platform

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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, &#39;SimpleXMLElement&#39;, 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;
    }
}

?>
Copy after login

More WeChat public platform message interface development from Hello2BizUser text to subscribe event. For related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)