PHP WeChat public platform development subscription event processing_PHP tutorial

WBOY
Release: 2016-07-13 10:36:21
Original
1090 people have browsed it

[PHP WeChat public platform development series]

01. Configure WeChat interface
02. Public platform sample code analysis
03. Subscription event (subscribe) processing

URL of this article: http://www.phpchina.com/archives/view-43367-1.html
This series is contributed by PHPChina's specially invited author @David_Tang. Please indicate the author's information and the address of this article when reprinting.

1. Introduction

When a new user follows the WeChat public platform, a subscription event will be generated, namely subscribe event. There is no corresponding reply to this event in the default code.

After new users pay attention to the public platform, they may want to know what functions the platform provides and how to use the platform. In layman's terms, it is the "instruction manual" of the platform.

This article will describe in detail the processing of the subscribe event and reply to the corresponding information to improve interactivity.

2. Idea analysis

WeChat currently provides five message types, namely:

  • Text message (text);
  • Picture message (image);
  • Geolocation message (location);
  • Link message (link);
  • Event push (event);

After receiving a message, you first need to make a judgment on the message type, and then process different types of messages. In event push, event types are divided into three types: subscribe (subscription), unsubscribe (cancel subscription), and CLICK (custom menu click event). One more judgment is required; after it is judged to be a subscribe event, the event will be determined according to the settings. Welcome message, reply to the user.

3. Determine the message type

PHP WeChat public platform development subscription event processing_PHP tutorial
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

switch($RX_TYPE)
{
    case "text":
        $resultStr = $this->handleText($postObj);
        break;
    case "event":
        $resultStr = $this->handleEvent($postObj);
        break;
    default:
        $resultStr = "Unknow msg type: ".$RX_TYPE;
        break;
}
Copy after login
PHP WeChat public platform development subscription event processing_PHP tutorial

Description:

$RX_TYPE = trim($postObj->MsgType); Get the message type;

case "text":
​$resultStr = $this->handleText($postObj); ​Use the handleText() function to process text messages;

case "event":
​$resultStr = $this->handleEvent($postObj); ​Use the handleEvent() function to handle event push;

4. Determine the event type

PHP WeChat public platform development subscription event processing_PHP tutorial
switch ($object->Event)
{
    case "subscribe":
        $contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
        break;
    default :
        $contentStr = "Unknow Event: ".$object->Event;
        break;
}
Copy after login
PHP WeChat public platform development subscription event processing_PHP tutorial

Description:

If it is a subscribe event, set the reply content as "Thank you for paying attention to [Zhuojin Suzhou]...";

>5. Complete code

PHP WeChat public platform development subscription event processing_PHP tutorial
<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "zhuojin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();

class wechatCallbackapiTest
{
    /*public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }*/

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract 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->handleText($postObj);
                        break;
                    case "event":
                        $resultStr = $this->handleEvent($postObj);
                        break;
                    default:
                        $resultStr = "Unknow msg type: ".$RX_TYPE;
                        break;
                }
                echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    public function handleText($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml>";             
        if(!empty( $keyword ))
        {
            $msgType = "text";
            $contentStr = "Welcome to wechat world!";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{
            echo "Input something...";
        }
    }

    public function handleEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
                break;
            default :
                $contentStr = "Unknow Event: ".$object->Event;
                break;
        }
        $resultStr = $this->responseText($object, $contentStr);
        return $resultStr;
    }
    
    public function responseText($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;
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
Copy after login
PHP WeChat public platform development subscription event processing_PHP tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/739147.htmlTechArticle[PHP WeChat public platform development series] 01. Configure WeChat interface 02. Public platform sample code analysis 03. Subscribe to events (subscribe) Address for processing this article: http://www.phpchina.com/archives...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!