: This article mainly introduces the keyword auto-reply code. if you are interested in the PHP Tutorial, refer to it.
/** Wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new WechatCallbackapiTest (); $ wechatObj-> valid (); $ wechatObj-> responseMsg (); class WechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ['echostr']; // valid signature, optionif ($ this-> checkSignature () {echo $ echoStr; exit;} public function responseMsg () {// get post data, May be due to the different environments $ post Str = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post dataif (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ tpUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ textTpl ="
%s
%s
% S
%s
%s
0
"; If (! Empty ($ keyword) {$ msgType = "text"; $ contentStr = "1 phone mailbox 2. qq information 3 address information "; if ($ keyword =" 1 ") {// when the keyword is 1 reply $ contentStr =" Phone: 15057190640 Email: 2323178881@qq.com !! ";} If ($ keyword =" 2 ") {// when the keyword is 2, reply $ contentStr =" My QQ is 2323178881! ";} If ($ keyword =" 3 ") {// when the keyword is 3, $ contentStr =" Hangzhou Jianggan district, Zhejiang province! ";}$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ ToUserName, $ time, $ msgType, $ contentStr); echo $ resultStr ;} else {echo "Input something... ";}} else {echo" "; exit ;}} 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 ;}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the keyword auto-reply code, including the content, hope to be helpful to friends who are interested in PHP tutorials.