WeChat public platform development follow/cancel follow event example

Source: Internet
Author: User
When you follow or remove the public account, the event is pushed to the URL entered by the developer. It is convenient for developers to send a welcome message to users or unbind accounts. Below is an example of public platform interest and interest cancellation :? Phpdefine (... when you follow and cancel the public account, this event is pushed to the URL entered by the developer. This allows developers to send welcome messages to users or unbind accounts.

The following is an example of paying attention to and removing attention from the public platform:

 ResponseMsg ();} else {$ wechatObj-> valid ();} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr; exit ;}} public function responseMsg () // execute the receiver method {$ 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 "event": $ result = $ this-> receiveEvent ($ postObj); breadk;} echo $ result;} else {echo ""; exit ;}} private function receiveEvent ($ object) {$ content = ""; switch ($ postObj-> Event) {case "subscribe": $ content = "Welcome to Wangzhi blog "; // Here is the prompt message break; case "unsubscribe": $ content = ""; break;} $ result = $ this-> transmitText ($ object, $ content); return $ result;} private function transmitText ($ object, $ content) {$ textTpl ="
        
  %s
         
  %s
         
  
   
% S
         
  text
         
  %s
         
  
   
0
         
 "; $ Result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> $ ToUserName, time (), $ content); return $ result ;} private function checkSignature () {$ signature =$ _ GET ["signature"]; $ timestamp =$ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ Signature) {return true;} else {return false ;}}?>

Code parameters:

Parameter description

ToUserName developer id

FromUserName sender account (an OpenID)

CreateTime message creation time (integer)

MsgType message type, event

Event type, subscribe and unsubscribe)

Related Article

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.