1407743423"; // 初始化WXBizMsgCrypt对象 $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $encryptMsg = ''; // 加密消息 $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg); if ($errCode == 0) { print("加密后: " . $encryptMsg . "\n"); } else { print($errCode . "\n"); } // 解析加密后的XML消息 $xml_tree = new DOMDocument(); $xml_tree->loadXML($encryptMsg); $array_e = $xml_tree->getElementsByTagName('Encrypt'); $array_s = $xml_tree->getElementsByTagName('MsgSignature'); $encrypt = $array_e->item(0)->nodeValue; $msg_sign = $array_s->item(0)->nodeValue; // 构造解密的XML格式 $format = ""; $from_xml = sprintf($format, $encrypt); // 解密消息 $msg = ''; $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); if ($errCode == 0) { print("解密后: " . $msg . "\n"); } else { print($errCode . "\n"); } 命名空间声明:namespace LaneWeChat\Core\Aes; 定义了类的命名空间,表明这个脚本属于LaneWeChat模块的Aes部分。 引入类库:include_once "wxBizMsgCrypt.php"; 引入用于消息加密和解密的类库。 初始化参数:定义了用于加密和解密的参数,包括 encodingAesKey、token、timeStamp、nonce 和 appId。 初始化WXBizMsgCrypt对象:创建 WXBizMsgCrypt 对象,用于消息的加密和解密。 加密消息:调用 encryptMsg 方法对消息进行加密,并输出加密后的结果。 解析加密后的XML消息:使用 DOMDocument 解析加密后的XML消息,提取 Encrypt 和 MsgSignature 的值。 构造解密的XML格式:构造用于解密的XML格式。 解密消息:调用 decryptMsg 方法对消息进行解密,并输出解密后的结果。