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

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

高洛峰
高洛峰Original
2017-03-04 14:49:561881browse

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

746d1c365ce1b149275c8e015d81db55responseMsg();

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 = "b2a0af5a8fd26276da50279a1c63a57a
ea5d8177d19f22584533e5c37c389942d3242fa0f72a59f12bbb2807edba61b76671a89dce89e879d9e9c6d81d03862b
c5123754d1f4829fae4905e8abb602f9d3242fa0f72a59f12bbb2807edba61b742a4b8d57eb0afadcf16b7a02c69caaf
246311df1688542638dc52b54a1a4c87%se660f1169ff44ea75c5a982fcb1cde61
42815c2206ae835d7fd68cb4ae21e4dffa796850a1cf5d7bc01ca8cd7f8b83de698463fc03844fbe5a9caafaa1ebd0b1
ea63b4477034504a08070acf4e0b68b2d3242fa0f72a59f12bbb2807edba61b7aa91fa7c40b1cd973268e154dae1a50e
c16e87311aa318186a2141a5b8ce5511%dfecc2d83bf66fbc799a88f710e0cd251
21118965b89073f60271ef4a3b5d3c58";
        $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!

Statement:
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