WeChat message body encrypted PHP version

Source: Internet
Author: User
Tags cdata
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;}}? >
  • Related Article

    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.