Update demo.php

src
pfspx4a7z 7 months ago
parent 8b77cea77e
commit 0841725b98

@ -10,9 +10,10 @@ $nonce = "xxxxxx";
$appId = "wxb11529c136998cb6"; $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>"; $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); $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$encryptMsg = ''; $encryptMsg = '';
// 加密消息
$errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg); $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
if ($errCode == 0) { if ($errCode == 0) {
print("加密后: " . $encryptMsg . "\n"); print("加密后: " . $encryptMsg . "\n");
@ -20,6 +21,7 @@ if ($errCode == 0) {
print($errCode . "\n"); print($errCode . "\n");
} }
// 解析加密后的XML消息
$xml_tree = new DOMDocument(); $xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg); $xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt'); $array_e = $xml_tree->getElementsByTagName('Encrypt');
@ -27,10 +29,11 @@ $array_s = $xml_tree->getElementsByTagName('MsgSignature');
$encrypt = $array_e->item(0)->nodeValue; $encrypt = $array_e->item(0)->nodeValue;
$msg_sign = $array_s->item(0)->nodeValue; $msg_sign = $array_s->item(0)->nodeValue;
// 构造解密的XML格式
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"; $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt); $from_xml = sprintf($format, $encrypt);
// 第三方收到公众号平台发送的消息 // 解密消息
$msg = ''; $msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) { if ($errCode == 0) {
@ -38,8 +41,11 @@ if ($errCode == 0) {
} else { } else {
print($errCode . "\n"); print($errCode . "\n");
} }
命名空间声明namespace LaneWeChat\Core; 定义了类的命名空间表明这个类属于LaneWeChat模块的核心部分。 命名空间声明namespace LaneWeChat\Core\Aes; 定义了类的命名空间表明这个脚本属于LaneWeChat模块的Aes部分。
类定义class Menu 定义了一个用于管理微信自定义菜单的类。 引入类库include_once "wxBizMsgCrypt.php"; 引入用于消息加密和解密的类库。
setMenu方法用于创建自定义菜单。处理菜单数据将一维数组转换为树形结构支持子菜单并发送POST请求到微信服务器。 初始化参数:定义了用于加密和解密的参数,包括 encodingAesKey、token、timeStamp、nonce 和 appId。
getMenu方法用于获取当前公众号的自定义菜单信息。发送GET请求到微信服务器。 初始化WXBizMsgCrypt对象创建 WXBizMsgCrypt 对象,用于消息的加密和解密。
delMenu方法用于删除当前公众号的自定义菜单。发送GET请求到微信服务器 加密消息:调用 encryptMsg 方法对消息进行加密,并输出加密后的结果。
解析加密后的XML消息使用 DOMDocument 解析加密后的XML消息提取 Encrypt 和 MsgSignature 的值。
构造解密的XML格式构造用于解密的XML格式。
解密消息:调用 decryptMsg 方法对消息进行解密,并输出解密后的结果。
Loading…
Cancel
Save