点击自定义菜单,拉取信息。
include_once "WXBizMsgCrypt.php";
$encodingAesKey = "";
$token = "";
$corpId ="";
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
<span style="white-space:pre"> </span>$sReqMsgSig = ($_GET['msg_signature']);
$sReqTimeStamp = ($_GET['timestamp']);
$sReqNonce = ($_GET['nonce']);
<span style="white-space:pre"> </span>$sPostData = $GLOBALS["HTTP_RAW_POST_DATA"];
<span style="white-space:pre"> </span>
$sMsg = ""; //解析之后的明文
$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sPostData, $sMsg);
if ($errCode == 0) {
$xml = new DOMDocument();
$xml->loadXML($sMsg);
$reqToUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
$reqFromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$reqCreateTime = $xml->getElementsByTagName('CreateTime')->item(0)->nodeValue;
$reqMsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;
$reqEvent = $xml->getElementsByTagName('Event')->item(0)->nodeValue;
$reqEventKey = $xml->getElementsByTagName('EventKey')->item(0)->nodeValue;
$reqAgentID = $xml->getElementsByTagName('AgentID')->item(0)->nodeValue;
switch($reqEventKey){
case "1":
$mycontent="我是马云";
break;
case "2":
$mycontent="我是马化腾";
break;
case "3":
$mycontent="我是马末末";
break;
}
$sRespData =
"<xml>
<ToUserName><![CDATA[".$reqFromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$corpId."]]></FromUserName>
<CreateTime>".$sReqTimeStamp."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$mycontent."]]></Content>
</xml>";
$sEncryptMsg = ""; //xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);
if ($errCode == 0) {
//file_put_contents('smg_response.txt', $sEncryptMsg); //debug:查看smg
print($sEncryptMsg);
} else {
print($errCode . "\n\n");
}
} else {
print($errCode . "\n\n");
}