You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace LaneWeChat\Core\Aes; // 定义命名空间,用于组织代码
class XMLParse // 定义XMLParse类
{
// 提取方法用于从XML文本中提取加密信息和接收方账号
public function extract($xmltext)
{
try {
$xml = new \DOMDocument(); // 创建DOMDocument对象
$xml->loadXML($xmltext); // 加载XML文本
// 使用DOMDocument对象的getElementsByTagName方法获取Encrypt和ToUserName标签
$array_e = $xml->getElementsByTagName('Encrypt');
$array_a = $xml->getElementsByTagName('ToUserName');
// 获取Encrypt标签的文本内容和ToUserName标签的文本内容
$encrypt = $array_e->item(0)->nodeValue;
$tousername = $array_a->item(0)->nodeValue;
// 返回提取结果包括状态码0表示成功加密信息和接收方账号
return array(0, $encrypt, $tousername);
} catch (\Exception $e) {
// 如果发生异常返回错误码和null值
return array(ErrorCode::$ParseXmlError, null, null);
}
}
// 生成方法用于生成XML格式的响应消息
public function generate($encrypt, $signature, $timestamp, $nonce)
{
// 定义XML格式的字符串模板使用CDATA区包含加密信息和其他参数
$format = "<xml>
<Encrypt><![CDATA[%s]]></Encrypt>
<MsgSignature><![CDATA[%s]]></MsgSignature>
<TimeStamp>%s</TimeStamp>
<Nonce><![CDATA[%s]]></Nonce>
</xml>";
// 使用sprintf函数格式化XML字符串模板插入加密信息和其他参数
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
}
}
?>