Message Interface development on WeChat public platform (26) from Hello2BizUser text to subscribe event

Source: Internet
Author: User
Tags php define
Public platform Message Interface Development (26) from Hello2BizUser text to subscribe event

The following changes will occur to the public platform due to interface upgrade. When a new user subscribes, the previous "Hello2BizUser" text will be pushed and changed to a "subscribe" event. Recently, I met several friends who asked the new user how to change the code after subscribing to the event. now I will explain the detailed process as follows:


I. handling Hello2BizUser old events

In the old follow events, after a user pays attention to the public platform account, the system will send a Hello2BizUser text to the public account, in the background development mode of a public account, you can use Hello2BizUser to send a welcome word.

The sample code is as follows:

If ($ keyword = "Hello2BizUser") {$ contentStr = "Welcome to Fang times studio. this is an old welcome word. you cannot receive it "; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag); return $ resultStr ;}

Modifying the basic interface will affect everyone. It is generally not easy to make such changes.

The disadvantage of this method is that if the user does not judge the event, there will be no welcome words. it does not matter, it does not affect the appearance of a welcome word. However, in many people's program code, all processes are directly judgment keywords. For example, we once saw a hospital account. when a user sends a registration number, the number of people listed above is displayed. However, the background program did not differentiate the number and sent Hello2BizUser as a registration ticket, the registration "Hello2BizUser" is not found, and the number of people in front is unknown. In addition, if a user actively sends a Hello2BizUser in the past, it will also get the same content as the welcome word, although few users will send this content.

On the other hand, making user attention an event is more conducive to the implementation of the statistical function. With this event, we can easily determine the number of followers and the number of Unsubscribe users. However, the original Hello2BizUser text push judgment may be inaccurate because users can manually send messages, generate false attention statistics.


II. "subscribe" subscription event judgment

Subscribe is a new event. First, we need to determine the event type. in the official example, we add a judgment on this event. the modifications are 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, the subscribe event is judged in the event receiving and processing function:

Private function receiveEvent ($ object) {$ contentStr = ""; switch ($ object-> Event) {case "subscribe": $ contentStr = "Hello, welcome to fanwe studio. New feeling, new experience! "; Break;} $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr ;}

In this way, the subscribe event is processed.


II. complete code

 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 functi On receiveText ($ object) {$ funcFlag = 0; $ keyword = trim ($ object-> Content); $ resultStr = ""; $ cityArray = array (); $ contentStr = ""; $ needArray = false; $ illegal = false; $ saytome = false; if ($ keyword = "Hello2BizUser ") {$ contentStr = "Welcome to fantimes studio. this is an old welcome word. you cannot receive it when you are paying attention to it."; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag); return $ resultStr;} else {}} private function receiveEvent ($ ob Ject) {$ contentStr = ""; switch ($ object-> Event) {case "subscribe": $ contentStr = "Hello, welcome to Fang times studio. New feeling, new experience! "; Break;} $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr;} private function transmitText ($ object, $ content, $ flag = 0) {$ textTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  text
  
  %s
  
  
   
% D
  
 "; $ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content, $ flag ); return $ resultStr ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.