The PHP public platform can determine based on the information sent by the user, and then give corresponding replies, with good interaction. The following describes how to simulate a simple reply function. Based on this case, developers can understand the interaction principle and perform further development. 1. Introduction
The PHP public platform can determine based on the information sent by the user, and then give corresponding replies, with good interaction. The following describes how to simulate a simple reply function. Based on this case, developers can understand the interaction principle and perform further development.
II. train of thought analysis
The text information sent by the user can be extracted by using the simple if... elseif... else.
The key code is as follows:
If ($ keyword = "") {$ contentStr = "hello";} elseif ($ keyword = "Suzhou") {$ contentStr = "there is heaven, suhang ";} else {$ contentStr =" thank you for choosing zhuojinsz ";}
If the user sends "hello", the user replies "hello". if the user sends "Suzhou", the user replies "there is heaven and Suhang". other information, then, reply to your welcome speech.
III. 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"; if ($ keyword = "hello") {$ contentStr = "hello ";} elseif ($ keyword = "Suzhou") {$ contentStr = "there is heaven, there is Suhang";} else {$ contentStr = "thank you for your attention to [Zhuo Jin Suzhou] No: zhuojinsz ";}$ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr ;} else {echo "Input something... ";}} public function handleEvent ($ object) {$ contentStr =" "; switch ($ objec T-> 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 ;}}?>IV. test