WeChat message body encryption php edition

Source: Internet
Author: User
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 ;}}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.