| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/12/16
- * Time: 9:30
- */
- namespace Yunshop\PhoneBill\service;
- class ThirdRequestPhoneBill
- {
- private $appSecret = '';
- private $appKey = '';
- public $callBackUrl = '';
- public $param = [];
- private $sign = '';
- public $serverRoot = '';
- public function __construct($appSecret, $appKey, $callBackUrl)
- {
- if (!empty($appSecret)) {
- $this->appSecret = $appSecret;
- }
- if (!empty($appKey)) {
- $this->appKey = $appKey;
- }
- if (!empty($callBackUrl)) {
- $this->callBackUrl = $callBackUrl;
- }
- }
- public function setParam($key, $value)
- {
- $this->param = array_merge($this->param, array($key => $value));
- }
- public function getSign()
- {
- $param = $this->filtrateParam();
- $sortParam = $this->sortParam($param);
- $sign = $this->createLinkstring($sortParam);
- $this->sign = $this->md5Sign($sign);
- return $this->sign;
- }
- /**
- * 过滤空值
- * @return array
- */
- private function filtrateParam()
- {
- $param = [];
- foreach ($this->param as $k => $v) {
- if ("" != $v) {
- $param[$k] = $v;
- }
- }
- return $param;
- }
- /**
- * 以键的ASCII从小到大排序
- * @param $param
- * @return mixed
- */
- private function sortParam($param)
- {
- ksort($param);
- reset($param);
- return $param;
- }
- /**
- * 转义
- * @param $para
- * @return bool|string
- */
- private function createLinkstring($para)
- {
- $arg = '';
- foreach ($para as $key => $val) {
- $arg .= $key . '=' . $val . '&';
- }
- //去掉最后一个&字符
- $arg = substr($arg, 0, count($arg) - 2);
- //如果存在转义字符,那么去掉转义
- if (get_magic_quotes_gpc()) {
- $arg = stripslashes($arg);
- }
- return $arg;
- }
- /**
- * 拼接私钥后加密
- * @param $prestr
- * @return string
- */
- private function md5Sign($prestr)
- {
- $prestr = $prestr . '&key=' . $this->appSecret;
- return strtoupper(md5($prestr));
- }
- public function getAppkey()
- {
- return $this->appKey;
- }
- }
|