jsonparse.php 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. include_once "errorCode.php";
  3. /**
  4. * JsonParse class
  5. *
  6. * 提供提取消息格式中的密文及生成回复消息格式的接口.
  7. */
  8. class JsonParse
  9. {
  10. /**
  11. * 提取出json数据包中的加密消息
  12. * @param string $jsontext 待提取的json字符串
  13. * @return string 提取出的加密消息字符串
  14. */
  15. public function extract($jsontext)
  16. {
  17. try {
  18. $encrypt = json_decode($jsontext, true)['Encrypt'];
  19. return array(0, $encrypt);
  20. } catch (Exception $e) {
  21. print $e . "\n";
  22. return array(ErrorCode::$ParseXmlError, null);
  23. }
  24. }
  25. /**
  26. * 生成json消息
  27. * @param string $encrypt 加密后的消息密文
  28. * @param string $signature 安全签名
  29. * @param string $timestamp 时间戳
  30. * @param string $nonce 随机字符串
  31. */
  32. public function generate($encrypt, $signature, $timestamp, $nonce)
  33. {
  34. $format = '{"encrypt": "%s", "msgsignature": "%s", "timestamp": "%s", "nonce": "%s"}';
  35. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  36. }
  37. }
  38. ?>