The message body encryption php version uses wx_sample.php and the encrypted demo. php is spliced. the Official wiki is poorly written, hard to understand, and the demo is not very good. null parameters are used in the class, which is hard for beginners to understand, it is better to directly obtain encryption and decryption. In addition, encryption and decryption are also performed logically, which is opposite to the processing process, resulting in difficulties in understanding.
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 =" token "; $ TOKEN = token; $ timestamp = $ _ GET [" timestamp "]; $ nonce = $ _ GET [" nonce "]; $ appId = "wx472248038524 43cc "; $ msg_sign = $ _ GET [" msg_signature "]; // decrypt $ pc = new WXBizMsgCrypt ($ token, $ encodingAesKey, $ appId); // get post data, may be 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) {/* 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 ="
%s
%s
% S
%s
%s
0
"; If (! Empty ($ keyword) {$ msgType = "text"; $ contentStr = "Welcome to wechat world! "; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); // encrypt $ encryptMsg = ''; $ errCode = $ pc-> encryptMsg ($ resultStr, $ timeStamp, $ nonce, $ encryptMsg); if ($ errCode = 0) {echo $ encryptMsg ;} else {print ($ errCode. "\ n") ;}} else {echo "Input something... ";}} else {echo" "; exit ;}} else {print ($ errCode. "\ n");} // extract post data} 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 rulesort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>