Home Backend Development PHP Tutorial WeChat public platform development (3) Subscription event (subscribe) processing_PHP tutorial

WeChat public platform development (3) Subscription event (subscribe) processing_PHP tutorial

Jul 20, 2016 am 11:13 AM
one three event focus on deal with Will platform develop WeChat new user user subscription

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.

WeChat currently provides five message types, namely:

  • Text message (text);
  • Image 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), CLICK (custom menu click event), and one more judgment is required; after it is judged to be a subscribe event, according to Set the welcome message and reply to the user.

<span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span  LIBXML_NOCDATA);
</span><span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType);

</span><span switch</span>(<span $RX_TYPE</span><span )
{
    </span><span case</span> "text":
        <span $resultStr</span> = <span $this</span>->handleText(<span $postObj</span><span );
        </span><span break</span><span ;
    </span><span case</span> "event":
        <span $resultStr</span> = <span $this</span>->handleEvent(<span $postObj</span><span );
        </span><span break</span><span ;
    </span><span default</span>:
        <span $resultStr</span> = "Unknow msg type: ".<span $RX_TYPE</span><span ;
        </span><span break</span><span ;
}</span>
Copy after login

Instructions:

$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;

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

Instructions:

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

<?<span php
</span><span /*</span><span *
  * wechat php test
  </span><span */</span>

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

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

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

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

        </span><span //</span><span extract post data</span>
        <span if</span> (!<span empty</span>(<span $postStr</span><span )){
                
                </span><span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span  LIBXML_NOCDATA);
                </span><span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType);

                </span><span switch</span>(<span $RX_TYPE</span><span )
                {
                    </span><span case</span> "text":
                        <span $resultStr</span> = <span $this</span>->handleText(<span $postObj</span><span );
                        </span><span break</span><span ;
                    </span><span case</span> "event":
                        <span $resultStr</span> = <span $this</span>->handleEvent(<span $postObj</span><span );
                        </span><span break</span><span ;
                    </span><span default</span>:
                        <span $resultStr</span> = "Unknow msg type: ".<span $RX_TYPE</span><span ;
                        </span><span break</span><span ;
                }
                </span><span echo</span> <span $resultStr</span><span ;
        }</span><span else</span><span  {
            </span><span echo</span> ""<span ;
            </span><span exit</span><span ;
        }
    }

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

    </span><span public</span> <span function</span> handleEvent(<span $object</span><span )
    {
        </span><span $contentStr</span> = ""<span ;
        </span><span switch</span> (<span $object</span>-><span Event)
        {
            </span><span case</span> "subscribe":
                <span $contentStr</span> = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待..."<span ;
                </span><span break</span><span ;
            </span><span default</span> :
                <span $contentStr</span> = "Unknow Event: ".<span $object</span>-><span Event;
                </span><span break</span><span ;
        }
        </span><span $resultStr</span> = <span $this</span>->responseText(<span $object</span>, <span $contentStr</span><span );
        </span><span return</span> <span $resultStr</span><span ;
    }
    
    </span><span public</span> <span function</span> responseText(<span $object</span>, <span $content</span>, <span $flag</span>=0<span )
    {
        </span><span $textTpl</span> = "<span <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml></span>"<span ;
        </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span>, <span $flag</span><span );
        </span><span return</span> <span $resultStr</span><span ;
    }

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

</span>?>
Copy after login

Please follow Zhuojin Suzhou WeChat public account, Zhuojin Suzhou is developed based on SAE platform and is targeted at mainstream WeChat functions Conduct development testing.

You can follow the Zhuojin Suzhou public account to conduct functional testing and obtain new application development.

1. Log in to the WeChat client, friends -> Add friends -> Search number -> zhuojinsz, find and follow.

2. Scan the QR code:

Zhuojin Suzhou Function list.


We Believe, Great People Share Knowledge...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440365.htmlTechArticleNew users following the WeChat public platform will generate a subscription event, that is, the subscribe event. There is no support for this in the default code. Respond to the event accordingly. Follow the public platform for new users...
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)

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

gateio Chinese official website gate.io trading platform website gateio Chinese official website gate.io trading platform website Feb 21, 2025 pm 03:06 PM

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

List of handling fees for okx trading platform List of handling fees for okx trading platform Feb 15, 2025 pm 03:09 PM

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

See all articles