BaseApi.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace addons\AvoidTax\common\jdk\xinlong;
  3. use yii\helpers\Json;
  4. /**
  5. * xinlong api
  6. * @package addons\AvoidTax\common\jdk\xinlong
  7. */
  8. class BaseApi extends \yii\base\BaseObject
  9. {
  10. // 获取token的url
  11. const TOKEN_URL = "https://xlapi.51xinlong.com/jwt/auth/openapi"; // 线上
  12. // const TOKEN_URL = "https://xlapi.51xinlong.com/ws-test/auth/openapi";
  13. // 接口api url
  14. const API_URL = "https://xlapi.51xinlong.com/openapi";// 线上
  15. // const API_URL = "https://xlapi.51xinlong.com/ws-test/rute";
  16. public $uid = "";
  17. public $companyid = "";
  18. public $appkey = "";
  19. public $appSecret = "";
  20. public $account = "";
  21. public $mainAccount = "";
  22. public function __construct($config = [])
  23. {
  24. parent::__construct($config);
  25. }
  26. /**
  27. * 获取token
  28. *
  29. * @return string
  30. */
  31. public function queryToken()
  32. {
  33. $appid = $this->appkey;
  34. $secret = $this->appSecret;
  35. $url = self::TOKEN_URL . '?' . http_build_query(compact('appid', 'secret'));
  36. $res = $this->httpPost([], $url);
  37. $data = Json::decode($res);
  38. return $data;
  39. }
  40. /**
  41. * POST请求接口方法
  42. * @param string $method
  43. * @param array $params
  44. * @return bool|string
  45. */
  46. public function requestPost(string $method,array $params = []) : array
  47. {
  48. $uri = self::API_URL . "?service={$method}";
  49. $header = [
  50. "Authorization:". "Bearer ".$this->token,
  51. "traceID:". uniqid(),
  52. "companyid:". $this->companyid,
  53. "mainAccount:". $this->mainAccount,
  54. "uid:". $this->uid
  55. ];
  56. $postUrl = $uri;
  57. $curlPost = [
  58. 'timestamp' => time() * 1000,
  59. 'appKey' => $this->appkey,
  60. 'version' => "V1.1.0",
  61. 'requestBody' => $this->encrypt($params),
  62. 'appSecret' => $this->appSecret
  63. ];
  64. $curlPost['signature'] = $this->getKey($curlPost);
  65. unset($curlPost['appSecret']);
  66. // 初始化curl
  67. $curl = curl_init();
  68. curl_setopt($curl, CURLOPT_URL, $postUrl);
  69. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  70. curl_setopt($curl, CURLOPT_HEADER, 0);
  71. // 要求结果为字符串且输出到屏幕上
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  74. curl_setopt($curl, CURLOPT_TIMEOUT,30);
  75. // post提交方式
  76. curl_setopt($curl, CURLOPT_POST, 1);
  77. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  78. // 运行curl
  79. $data = curl_exec($curl);
  80. curl_close($curl);
  81. $data = json_decode($data,true);
  82. // echo "<pre>";
  83. // echo "<h1>请求连接</h1>";
  84. // var_dump($uri);
  85. // echo "<h1>响应</h1>";
  86. // var_dump($data);
  87. // echo "<h1>业务参数</h1>";
  88. // var_dump($params);
  89. // echo json_encode($params);
  90. // echo "<h1>请求数据</h1>";
  91. // var_dump($curlPost);
  92. // echo json_encode($curlPost);
  93. // echo "<h1>请求头</h1>";
  94. // var_dump($header);
  95. // 非0000请求失败信息提示
  96. if (isset($data['code']) && $data['code'] != '0000') {
  97. throw new \Exception($data['message']);
  98. }
  99. return $data;
  100. }
  101. /**
  102. * 获取签名
  103. * @param array $params
  104. * @return string
  105. */
  106. protected function getKey(array $params) : string
  107. {
  108. ksort($params, SORT_NATURAL | SORT_FLAG_CASE);//排序不区分大小写
  109. $str = '';
  110. foreach ($params as $k => $v) {
  111. if ($k == 'signature' || $k == 'requestBody') continue;
  112. $str .= $k . $v;
  113. }
  114. $str = 'key' . $str . 'secret';
  115. // 对该字符串进行 SHA1 运算,得到一个十六进制的数
  116. $encodeStr = strtoupper(sha1($str));
  117. return $encodeStr;
  118. }
  119. /**
  120. * 获取加密数据
  121. * @param array $params
  122. * @return string
  123. */
  124. public function encrypt(array $params) : string
  125. {
  126. $requestBody = $params;
  127. $key = $this->appSecret;
  128. $body = json_encode($requestBody);
  129. $l = strlen($key);
  130. if ($l < 16)
  131. $key = str_repeat($key, ceil(16 / $l));
  132. if ($m = strlen($body) % 16)
  133. $body .= str_repeat("\x00", 16 - $m);
  134. return base64_encode(openssl_encrypt($body, "AES-128-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $key));
  135. }
  136. public function decrypt($str)
  137. {
  138. $key = $this->appSecret;
  139. $l = strlen($key);
  140. if ($l < 16)
  141. $key = str_repeat($key, ceil(16 / $l));
  142. return (openssl_decrypt(base64_decode($str), "AES-128-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $key));
  143. }
  144. private function httpPost($data,$url,$header = [])
  145. {
  146. $ch = curl_init();
  147. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  148. curl_setopt($ch,CURLOPT_TIMEOUT,600);
  149. curl_setopt($ch,CURLOPT_URL,$url);
  150. curl_setopt($ch,CURLOPT_POST,true);
  151. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  152. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  153. if ( !empty($header) ) {
  154. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  155. }
  156. if (strpos($url, 'https') !== false) {
  157. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  158. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  159. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  160. }
  161. $ret_data = trim(curl_exec($ch));
  162. curl_close($ch);
  163. return $ret_data;
  164. }
  165. }