<?php include_once "./demo/WXBizMsgCrypt.php"; // 假设企业号在公众平台上设置的参数如下 url_valid(); decrypt_msg(); function WXBizMsgCrypt_start() { $encodingAesKey = "tRx1Mg798ImKu3N3YerBjKB5pJwNem1mfwv9BEDT4lz"; $token = "9gjZHnJ"; $corpId = "wx83f0d0a2b0d3d571"; return $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); } //公众号服务器数据url验证 function url_valid() { $sVerifyMsgSig = $_GET['msg_signature']; $sVerifyTimeStamp = $_GET['timestamp']; $sVerifyNonce = $_GET['nonce']; $sVerifyEchoStr = $_GET['echostr']; if($sVerifyEchoStr) { $wxcpt = WXBizMsgCrypt_start(); $sEchoStr = ""; $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr); if($errCode == 0) { logger("R ".$sEchoStr);//写入日志文件 print($sEchoStr); } else { print($errCode . " "); } } } //xml解密 function decrypt_msg() { $sReqMsgSig = $_GET['msg_signature']; $sReqTimeStamp = $_GET['timestamp']; $sReqNonce = $_GET['nonce']; $sReqData = $GLOBALS['HTTP_RAW_POST_DATA']; $sMsg = ""; // 解析之后的明文 //判断收到信息为真 if($sReqData) { $wxcpt = WXBizMsgCrypt_start(); $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); //xml解密 if($errCode == 0) { $xml = new DOMDocument(); $xml->loadXML($sMsg); $Content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;//得到消息内容 $MsyType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;//得到消息类型 $FromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; //发送用户的id $MsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue; //消息类型 $CreateTime = $xml->getElementsByTagName('CreateTime')->item(0)->nodeValue; $MsgId = $xml->getElementsByTagName('MsgId')->item(0)->nodeValue; $AgentID = $xml->getElementsByTagName('AgentID')->item(0)->nodeValue; logger("R ".$sMsg);//写入日志文件 encrypt_msg($sReqNonce,$Content); }else { print("ERR: " . $errCode . " "); } } } //重新加密xml数据 function encrypt_msg($sReqNonce,$Content) { $sRespData ="<xml><ToUserName><![CDATA[".$FromUserName."]]></ToUserName> <FromUserName><![CDATA[wx83f0d0a2b0d3d571]]></FromUserName> <CreateTime>1348831860</CreateTime><MsgType> <![CDATA[text]]></MsgType> <Content><![CDATA[".$Content."]]></Content> <MsgId>1234567890123456</MsgId> <AgentID>128</AgentID> </xml>"; $sEncryptMsg = ""; //xml格式的密文 $sReqTimeStamp=time(); $wxcpt = WXBizMsgCrypt_start(); $errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg); if($errCode == 0) { print($sEncryptMsg); logger("R ".$sEncryptMsg);//写入日志文件 }else { print($errCode . " "); } } //写日志 function logger($log_content) { $max_size = 1000000; $log_filename = "log.xml"; if( file_exists($log_filename) && (abs(filesize($log_filename))>$max_size) ) { unlink($log_filename); } file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content." ", FILE_APPEND); } ?>
//封装成类库代码如下 <?php include_once "./demo/WXBizMsgCrypt.php"; // 假设企业号在公众平台上设置的参数如下 $qy_callback = new weixin_qy_callback(); $qy_callback->url_valid(); $qy_callback->decrypt_msg(); class weixin_qy_callback { public $encodingAesKey = "tRx1Mg798ImKu3N3YerBjKB5pJwNem1mfwv9BEDT4lz"; public $token = "9gjZHnJ"; public $corpId = "wx83f0d0a2b0d3d571"; public $wxcpt; public function __construct() { $this->wxcpt = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->corpId); } //公众号服务器数据url验证 public function url_valid() { $sVerifyMsgSig = $_GET['msg_signature']; $sVerifyTimeStamp = $_GET['timestamp']; $sVerifyNonce = $_GET['nonce']; $sVerifyEchoStr = $_GET['echostr']; if($sVerifyEchoStr) { $sEchoStr = ""; $errCode = $this->wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr); if($errCode == 0) { $this->logger("R ".$sEchoStr);//写入日志文件 print($sEchoStr); } else { print($errCode . " "); } } } //xml解密 public function decrypt_msg() { $sReqMsgSig = $_GET['msg_signature']; $sReqTimeStamp = $_GET['timestamp']; $sReqNonce = $_GET['nonce']; $sReqData = $GLOBALS['HTTP_RAW_POST_DATA']; $sMsg = ""; // 解析之后的明文 //判断收到信息为真 if($sReqData) { $errCode = $this->wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); //xml解密 if($errCode == 0) { $xml = new DOMDocument(); $xml->loadXML($sMsg); $Content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;//得到消息内容 $MsyType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;//得到消息类型 $FromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; //发送用户的id $MsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue; //消息类型 $CreateTime = $xml->getElementsByTagName('CreateTime')->item(0)->nodeValue; $MsgId = $xml->getElementsByTagName('MsgId')->item(0)->nodeValue; $AgentID = $xml->getElementsByTagName('AgentID')->item(0)->nodeValue; $this->logger("R ".$sMsg);//写入日志文件 $this->encrypt_msg($sReqNonce,$Content); }else { print("ERR: " . $errCode . " "); } } } //重新加密xml数据 public function encrypt_msg($sReqNonce,$Content) { $sRespData ="<xml><ToUserName><![CDATA[".$FromUserName."]]></ToUserName> <FromUserName><![CDATA[wx83f0d0a2b0d3d571]]></FromUserName> <CreateTime>1348831860</CreateTime><MsgType> <![CDATA[text]]></MsgType> <Content><![CDATA[".$Content."]]></Content> <MsgId>1234567890123456</MsgId> <AgentID>128</AgentID> </xml>"; $sEncryptMsg = ""; //xml格式的密文 $sReqTimeStamp=time(); $errCode = $this->wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg); if($errCode == 0) { print($sEncryptMsg); $this->logger("R ".$sEncryptMsg);//写入日志文件 }else { print($errCode . " "); } } //写日志 public function logger($log_content) { $max_size = 1000000; $log_filename = "log.xml"; if( file_exists($log_filename) && (abs(filesize($log_filename))>$max_size) ) { unlink($log_filename); } file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content." ", FILE_APPEND); } } ?>