1 Star 0 Fork 0

x135356 / sample-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
xmlparse.php 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
namespace Lonban\SampleCode;
use DOMDocument;
use Exception;
/**
* XMLParse class
*
* 提供提取消息格式中的密文及生成回复消息格式的接口.
*/
class XMLParse
{
/**
* 提取出xml数据包中的加密消息
* @param string $xmltext 待提取的xml字符串
* @return array 提取出的加密消息字符串
*/
public function extract($xmltext)
{
libxml_disable_entity_loader(true);
try {
$xml = new DOMDocument();
$xml->loadXML($xmltext);
$array_e = $xml->getElementsByTagName('Encrypt');
$array_a = $xml->getElementsByTagName('ToUserName');
$encrypt = $array_e->item(0)->nodeValue;
$tousername = $array_a->item(0)->nodeValue;
return array(0, $encrypt, $tousername);
} catch (Exception $e) {
//print $e . "\n";
return array(ErrorCode::$ParseXmlError, null, null);
}
}
/**
* 生成xml消息
* @param string $encrypt 加密后的消息密文
* @param string $signature 安全签名
* @param string $timestamp 时间戳
* @param string $nonce 随机字符串
*/
public function generate($encrypt, $signature, $timestamp, $nonce)
{
$format =
"<xml>
<Encrypt><![CDATA[%s]]></Encrypt>
<MsgSignature><![CDATA[%s]]></MsgSignature>
<TimeStamp>%s</TimeStamp>
<Nonce><![CDATA[%s]]></Nonce>
</xml>";
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
}
}
PHP
1
https://gitee.com/X135356/sample-code.git
git@gitee.com:X135356/sample-code.git
X135356
sample-code
sample-code
master

搜索帮助