<?php
include "../libs/weixin/WXBizMsgCrypt.php";
// 参数设置
$encodingAesKey = "2NakyiOvqqTIDW8GPUVnVxnCBO2mQIRsBQXuWsQWLCN";
$token = "iuk83T8ejUzJLmADW6C4";
$corpId = "请将这里修改为自己的企业corpID";
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
// 获取URL参数
$Xsignature = $_GET['msg_signature'];
$Xtimestamp = $_GET['timestamp'];
$Xnonce = $_GET['nonce'];
// 获取用POST的消息 [获取到的消息为XML格式的密文]
$userPostData = $GLOBALS["HTTP_RAW_POST_DATA"];
// 明文用户消息 [解密后的明文XML格式]
$userMessage = "";
// 解析之后的明文
$code = $wxcpt->DecryptMsg($Xsignature, $Xtimestamp, $Xnonce, $userPostData, $userMessage);
// 判断解密结果
if($code == 0) {
$postStr = $userMessage;
// 解析明文XML
$userMessageObject = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$toUserName = $userMessageObject->ToUserName;
$fromUserName = $userMessageObject->FromUserName;
$createTime = $userMessageObject->CreateTime;
$msgType = $userMessageObject->MsgType;
$content = trim($userMessageObject->Content);
$msgId = $userMessageObject->MsgId;
$agentID = $userMessageObject->AgentID;
// 回复消息的XML模板
$xmlTemplate = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
// 设置回复数据的明文数据
$toUser = $fromUserName;
$fromUser = $toUserName;
$text = "你好,欢迎来到企业号!西安格外科技欢迎你";
$type = $msgType;
$time = time();
// 组装明文XML模板数据,等待加密
$resultXML = sprintf($xmlTemplate, $toUser, $fromUser, $time, $type, $text);
// 加密后的XML数据
$responseData = "";
// 通过微信加密库加密
$code = $wxcpt->EncryptMsg($resultXML, $Xtimestamp, $Xnonce, $responseData);
// 判断加密结果
if($code == 0) {
echo $responseData;
} else {
echo "Weixin EncryptMsg Error Code: " . $code;
}
} else {
echo "Weixin DecryptMsg Error Code: " . $code;
}
?>