ThirdRequestPhoneBill.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/12/16
  6. * Time: 9:30
  7. */
  8. namespace Yunshop\PhoneBill\service;
  9. class ThirdRequestPhoneBill
  10. {
  11. private $appSecret = '';
  12. private $appKey = '';
  13. public $callBackUrl = '';
  14. public $param = [];
  15. private $sign = '';
  16. public $serverRoot = '';
  17. public function __construct($appSecret, $appKey, $callBackUrl)
  18. {
  19. if (!empty($appSecret)) {
  20. $this->appSecret = $appSecret;
  21. }
  22. if (!empty($appKey)) {
  23. $this->appKey = $appKey;
  24. }
  25. if (!empty($callBackUrl)) {
  26. $this->callBackUrl = $callBackUrl;
  27. }
  28. }
  29. public function setParam($key, $value)
  30. {
  31. $this->param = array_merge($this->param, array($key => $value));
  32. }
  33. public function getSign()
  34. {
  35. $param = $this->filtrateParam();
  36. $sortParam = $this->sortParam($param);
  37. $sign = $this->createLinkstring($sortParam);
  38. $this->sign = $this->md5Sign($sign);
  39. return $this->sign;
  40. }
  41. /**
  42. * 过滤空值
  43. * @return array
  44. */
  45. private function filtrateParam()
  46. {
  47. $param = [];
  48. foreach ($this->param as $k => $v) {
  49. if ("" != $v) {
  50. $param[$k] = $v;
  51. }
  52. }
  53. return $param;
  54. }
  55. /**
  56. * 以键的ASCII从小到大排序
  57. * @param $param
  58. * @return mixed
  59. */
  60. private function sortParam($param)
  61. {
  62. ksort($param);
  63. reset($param);
  64. return $param;
  65. }
  66. /**
  67. * 转义
  68. * @param $para
  69. * @return bool|string
  70. */
  71. private function createLinkstring($para)
  72. {
  73. $arg = '';
  74. foreach ($para as $key => $val) {
  75. $arg .= $key . '=' . $val . '&';
  76. }
  77. //去掉最后一个&字符
  78. $arg = substr($arg, 0, count($arg) - 2);
  79. //如果存在转义字符,那么去掉转义
  80. if (get_magic_quotes_gpc()) {
  81. $arg = stripslashes($arg);
  82. }
  83. return $arg;
  84. }
  85. /**
  86. * 拼接私钥后加密
  87. * @param $prestr
  88. * @return string
  89. */
  90. private function md5Sign($prestr)
  91. {
  92. $prestr = $prestr . '&key=' . $this->appSecret;
  93. return strtoupper(md5($prestr));
  94. }
  95. public function getAppkey()
  96. {
  97. return $this->appKey;
  98. }
  99. }