Php public platform development (3) Subscription event processing and public subscription
I. Introduction
When a new user pays attention to the public platform, a subscription event is generated, that is, a subscribe event. By default, no response is sent to this event in the code.
After a new user pays attention to the public platform, he may want to know What functions the platform provides and how to use the platform. Generally speaking, it is the "User Manual" of the platform ".
This article describes how to handle subscribe events in detail, and replies to relevant information to improve interaction.
Ii. Train of Thought Analysis
Currently, five message types are provided:
- Text message (text );
- Image message (image );
- Location );
- Link message );
- Event push (event );
After receiving a message, you must first determine the Message Type and then process different types of messages. In event push, there are three types of events: subscribe (subscription), unsubscribe (unsubscribe), and CLICK (custom menu CLICK Event). Further judgment is required; after being determined as a subscribe event, send a response to the user based on the set welcome message.
Iii. Determine the Message Type
$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;}
Note:
$ RX_TYPE = trim ($ postObj-> MsgType); get the message type; case "text": $ resultStr = $ this-> handleText ($ postObj); Use handleText () function to process text messages; case "event": $ resultStr = $ this-> handleEvent ($ postObj); Use the handleEvent () function to process event push;
Iv. Determine the Event Type
Switch ($ object-> Event) {case "subscribe": $ contentStr = "thank you for choosing [zhuojin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, famous city Suzhou, we provide you with Suzhou local life guide, Suzhou related information query, the best Suzhou platform. ". "\ N ". "The current platform functions are as follows :". "\ n ". "[1] check the weather. For example, enter Suzhou weather ". "\ n ". "[2] Check the public transit. For example, enter: Suzhou Public Transit 178 ". "\ n ". "[3] Translation, such as input: translation I love you ". "\ n ". "[4] Suzhou information query, such as: Suzhou guanqian Street ". "\ n ". "For more information, please wait... "; break; default: $ contentStr =" Unknow Event :". $ object-> Event; break ;}
Note:
In the case of a subscribe event, set the reply content to "Thank you for your attention [Zhuo Jin Suzhou ]...";
V. complete code
<? Php/*** wechat php test * // define your tokendefine ("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 dif Ferent environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract 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-> 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 h AndleText ($ 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 = "thank you for choosing zhuojin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, famous city Suzhou, we provide you with Suzhou local life guide, Suzhou related information query, the best Suzhou platform. ". "\ N ". "The current platform functions are as follows :". "\ n ". "[1] check the weather. For example, enter Suzhou weather ". "\ n ". "[2] Check the public transit. For example, enter: Suzhou Public Transit 178 ". "\ n ". "[3] Translation, such as input: translation I love you ". "\ n ". "[4] Suzhou information query, such as: Suzhou guanqian Street ". "\ n ". "For more information, please wait... "; 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 ;}}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.