Develop and subscribe to event processing on the PHP public platform. [PHP public platform development series] 01. configure interface 02. public platform sample code analysis 03. subscribe: www. phpchina. comarchives [PHP public platform development series] 01. configuration interface
02. public platform sample code analysis
03. subscribe processing
Address: http://www.phpchina.com/archives/view-43367-1.html
This series is provided by PHPChina's special Author @ David _tang. For more information, see the author information and the address of this article. I. INTRODUCTION
When a new user follows the public platform, a subscription event is generated, that isSubscribe EventBy default, no corresponding response is made 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); obtain 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 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 ]...";
> 5. complete code
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, '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 handleText ($ postObj) {$ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ textTpl ="
%s
%s
% S
%s
%s
0
"; 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 ="
%s
%s
% S
text
%s
% D
"; $ 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 ;}}?>
01. configure the interface 02. public platform sample code analysis 03. subscribe event (subscribe) handle this article address: http://www.phpchina.com/archives...