Using wx_sample.php and encrypted demo.php mosaic, the official wiki is poor, difficult to understand, the demo is not very good, the use of empty parameters in the process of assignment, beginners are difficult to understand, rather than direct encryption and decryption convenience. In addition, it also logically writes encryption and decryption, which is opposite to the processing process, which makes understanding difficult.
Responsemsg (); class Wechatcallbackapitest{public function valid () {$echoStr = $_get["Echostr"]; Valid signature, option if ($this->checksignature ()) {echo $echoStr; Exit }} Public Function responsemsg () {include_once "wxbizmsgcrypt.php"; $encodingAesKey = "abcdefghijklmnopqrstuvwxyz 0123456789ABCDEFG "; $token = token; $timestamp = $_get[" timestamp "]; $nonce = $_get[" nonce "]; $appId =" wx47224801062443cc "; $msg _sign = $_get[" Msg_signature "];//decrypt $pc = new Wxbizmsgcrypt ($token, $encodingAesKey, $appId);//get post data, May is Due to the different environments$poststr = $GLOBALS ["Http_raw_post_data"]; $msg = "; $errCode = $pc->decryptmsg ($msg _ Sign, $timeStamp, $nonce, $postStr, $msg), if ($errCode = = 0) {$postStr = $msg; if (!empty ($POSTSTR)) {/* LIBXM L_disable_entity_loader is to prevent XML eXternal entity injection, the best by check the Validi Ty 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 = "
%s
%s
%s
%s
%s
0
"; if (!empty ($keyword)) {$msgType = "text"; $CONTENTSTR = "Welcome to WeChat world!"; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);//encryption $encryptmsg = "; $errCode = $PC->encryptmsg ($resultStr, $timeStamp, $nonce, $ENCRYPTMSG); if ($errCode = = 0) {echo $encryptMsg;} else {print ($errC Ode. "\ n");} }else{echo "Input something ..."; }}else {echo ""; Exit }} else {print ($errCode. "\ n");} Extract Post data}private function Checksignature () {//You must define tokens 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_stringRulesort ($TMPARR, sort_string), $tmpStr = Implode ($TMPARR), $tmpStr = SHA1 ($TMPSTR), if ($tmpStr = = $signature) {return T Rue;} Else{return false;}}? >