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.

49 lines
1.3 KiB

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