Text messages developed by public accounts automatically reply to php code,
This article provides examples of php text message auto-reply codes for your reference. The specific content is as follows:
1. Download PHP sample code
1: http://xiazai.jb51.net/201608/yuanma/phpwx (jb51.netw..rar
2: https://mp.weixin.qq.com/wiki/home/index.html (start development-access guide-PHP sample code download)
2. wx_sample.php initial code
<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$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)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $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..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
3. Call the reply Information Method
Comment out $ wechatObj-> valid (); In the wx_sample.php file, and add "$ wechatObj-> responseMsg ();" under it ();".
<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); // $ wechatObj-> valid (); // Interface Verification $ wechatObj-> responseMsg (); // call the reply message method 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) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ t ExtTpl = "<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... ";}} else {echo" "; exit ;}} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>
4. Automatic keyword reply and follow-up reply
$ KeywordSaves the text information sent from the client.
Official developer documentation: https://mp.weixin.qq.com/wiki/home/index.html (Message Management-receive messages-receive event push-1. Follow/remove follow events)
The following event is different from the common text message. One is that the MsgType value is Event, and the other is that the added event value is subscribe. Because this parameter is not extracted from the official documentation (the original wx_sample.php), We need to extract it ourselves. Add two variables $ msgType and $ event to the program.
<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); // $ wechatObj-> valid (); // Interface Verification $ wechatObj-> responseMsg (); // call the reply message method 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) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ m SgType = $ postObj-> MsgType; // Message Type $ event = $ postObj-> Event; // time type, subscribe, unsubscribe) $ 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> "; switch ($ msgType) {case" event ": if ($ event = "subscribe") {$ contentStr = "Hi, please pay attention to haixian department store! ". "\ N ". "reply to the number '1' to learn about the store address. ". "\ n ". "reply to the number '2' to learn about the product type. ";} break; case" text ": switch ($ keyword) {case" 1 ": $ contentStr =" shop address :". "\ n ". "The first row of basement in the xindongsheng market, No. 233, Lushan West Road, jianggan city, Hangzhou. "; break; case" 2 ": $ contentStr =" item type :". "\ n ". "cup, bowl, cotton swab, bucket, waste bin, towel (Brush), mop, broom ,". "hangers, hooks, toothpicks, garbage bags, fresh-keeping bags (film), scissors, fruit knives, lunch boxes, etc. "; break; default: $ contentStr =" sorry, I will reply to your content later ";} break;} $ msgType =" text "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ Time, $ msgType, $ contentStr); echo $ resultStr;} else {echo ""; exit ;}} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ 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.