xmlparse.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. include_once "errorCode.php";
  8. /**
  9. * XMLParse class
  10. *
  11. * 提供提取消息格式中的密文及生成回复消息格式的接口.
  12. */
  13. class XMLParse
  14. {
  15. /**
  16. * 提取出xml数据包中的加密消息
  17. * @param string $xmltext 待提取的xml字符串
  18. * @return string 提取出的加密消息字符串
  19. */
  20. public function extract($xmltext)
  21. {
  22. try {
  23. $xml = new DOMDocument();
  24. $xml->loadXML($xmltext);
  25. $array_e = $xml->getElementsByTagName('Encrypt');
  26. $array_a = $xml->getElementsByTagName('ToUserName');
  27. $encrypt = $array_e->item(0)->nodeValue;
  28. $tousername = $array_a->item(0)->nodeValue;
  29. return array(0, $encrypt, $tousername);
  30. } catch (Exception $e) {
  31. //print $e . "\n";
  32. return array(ErrorCode::$ParseXmlError, null, null);
  33. }
  34. }
  35. /**
  36. * 生成xml消息
  37. * @param string $encrypt 加密后的消息密文
  38. * @param string $signature 安全签名
  39. * @param string $timestamp 时间戳
  40. * @param string $nonce 随机字符串
  41. */
  42. public function generate($encrypt, $signature, $timestamp, $nonce)
  43. {
  44. $format = "<xml>
  45. <Encrypt><![CDATA[%s]]></Encrypt>
  46. <MsgSignature><![CDATA[%s]]></MsgSignature>
  47. <TimeStamp>%s</TimeStamp>
  48. <Nonce><![CDATA[%s]]></Nonce>
  49. </xml>";
  50. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  51. }
  52. }
  53. ?>