Update demo.php

src
pfspx4a7z 7 months ago
parent 8b77cea77e
commit 0841725b98

@ -10,16 +10,18 @@ $nonce = "xxxxxx";
$appId = "wxb11529c136998cb6";
$text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
// 初始化WXBizMsgCrypt对象
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$encryptMsg = '';
// 加密消息
$errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
if ($errCode == 0) {
print("加密后: " . $encryptMsg . "\n");
print("加密后: " . $encryptMsg . "\n");
} else {
print($errCode . "\n");
print($errCode . "\n");
}
// 解析加密后的XML消息
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
@ -27,19 +29,23 @@ $array_s = $xml_tree->getElementsByTagName('MsgSignature');
$encrypt = $array_e->item(0)->nodeValue;
$msg_sign = $array_s->item(0)->nodeValue;
// 构造解密的XML格式
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
// 第三方收到公众号平台发送的消息
// 解密消息
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) {
print("解密后: " . $msg . "\n");
print("解密后: " . $msg . "\n");
} else {
print($errCode . "\n");
print($errCode . "\n");
}
命名空间声明namespace LaneWeChat\Core; 定义了类的命名空间表明这个类属于LaneWeChat模块的核心部分。
类定义class Menu 定义了一个用于管理微信自定义菜单的类。
setMenu方法用于创建自定义菜单。处理菜单数据将一维数组转换为树形结构支持子菜单并发送POST请求到微信服务器。
getMenu方法用于获取当前公众号的自定义菜单信息。发送GET请求到微信服务器。
delMenu方法用于删除当前公众号的自定义菜单。发送GET请求到微信服务器
命名空间声明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 方法对消息进行解密,并输出解密后的结果。
Loading…
Cancel
Save