| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace addons\AvoidTax\common\jdk\xinlong;
- use yii\helpers\Json;
- /**
- * xinlong api
- * @package addons\AvoidTax\common\jdk\xinlong
- */
- class BaseApi extends \yii\base\BaseObject
- {
- // 获取token的url
- const TOKEN_URL = "https://xlapi.51xinlong.com/jwt/auth/openapi"; // 线上
- // const TOKEN_URL = "https://xlapi.51xinlong.com/ws-test/auth/openapi";
- // 接口api url
- const API_URL = "https://xlapi.51xinlong.com/openapi";// 线上
- // const API_URL = "https://xlapi.51xinlong.com/ws-test/rute";
- public $uid = "";
- public $companyid = "";
- public $appkey = "";
- public $appSecret = "";
- public $account = "";
- public $mainAccount = "";
- public function __construct($config = [])
- {
- parent::__construct($config);
- }
- /**
- * 获取token
- *
- * @return string
- */
- public function queryToken()
- {
- $appid = $this->appkey;
- $secret = $this->appSecret;
- $url = self::TOKEN_URL . '?' . http_build_query(compact('appid', 'secret'));
- $res = $this->httpPost([], $url);
- $data = Json::decode($res);
- return $data;
- }
- /**
- * POST请求接口方法
- * @param string $method
- * @param array $params
- * @return bool|string
- */
- public function requestPost(string $method,array $params = []) : array
- {
- $uri = self::API_URL . "?service={$method}";
- $header = [
- "Authorization:". "Bearer ".$this->token,
- "traceID:". uniqid(),
- "companyid:". $this->companyid,
- "mainAccount:". $this->mainAccount,
- "uid:". $this->uid
- ];
- $postUrl = $uri;
- $curlPost = [
- 'timestamp' => time() * 1000,
- 'appKey' => $this->appkey,
- 'version' => "V1.1.0",
- 'requestBody' => $this->encrypt($params),
- 'appSecret' => $this->appSecret
- ];
- $curlPost['signature'] = $this->getKey($curlPost);
- unset($curlPost['appSecret']);
- // 初始化curl
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $postUrl);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- // 要求结果为字符串且输出到屏幕上
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_TIMEOUT,30);
- // post提交方式
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
- // 运行curl
- $data = curl_exec($curl);
- curl_close($curl);
- $data = json_decode($data,true);
- // echo "<pre>";
- // echo "<h1>请求连接</h1>";
- // var_dump($uri);
- // echo "<h1>响应</h1>";
- // var_dump($data);
- // echo "<h1>业务参数</h1>";
- // var_dump($params);
- // echo json_encode($params);
- // echo "<h1>请求数据</h1>";
- // var_dump($curlPost);
- // echo json_encode($curlPost);
- // echo "<h1>请求头</h1>";
- // var_dump($header);
- // 非0000请求失败信息提示
- if (isset($data['code']) && $data['code'] != '0000') {
- throw new \Exception($data['message']);
- }
- return $data;
- }
- /**
- * 获取签名
- * @param array $params
- * @return string
- */
- protected function getKey(array $params) : string
- {
- ksort($params, SORT_NATURAL | SORT_FLAG_CASE);//排序不区分大小写
- $str = '';
- foreach ($params as $k => $v) {
- if ($k == 'signature' || $k == 'requestBody') continue;
- $str .= $k . $v;
- }
- $str = 'key' . $str . 'secret';
- // 对该字符串进行 SHA1 运算,得到一个十六进制的数
- $encodeStr = strtoupper(sha1($str));
- return $encodeStr;
- }
- /**
- * 获取加密数据
- * @param array $params
- * @return string
- */
- public function encrypt(array $params) : string
- {
- $requestBody = $params;
- $key = $this->appSecret;
- $body = json_encode($requestBody);
- $l = strlen($key);
- if ($l < 16)
- $key = str_repeat($key, ceil(16 / $l));
- if ($m = strlen($body) % 16)
- $body .= str_repeat("\x00", 16 - $m);
- return base64_encode(openssl_encrypt($body, "AES-128-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $key));
- }
- public function decrypt($str)
- {
- $key = $this->appSecret;
- $l = strlen($key);
- if ($l < 16)
- $key = str_repeat($key, ceil(16 / $l));
- return (openssl_decrypt(base64_decode($str), "AES-128-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $key));
- }
- private function httpPost($data,$url,$header = [])
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
- curl_setopt($ch,CURLOPT_TIMEOUT,600);
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_POST,true);
- curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
- if ( !empty($header) ) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- if (strpos($url, 'https') !== false) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSLVERSION, 1);
- }
- $ret_data = trim(curl_exec($ch));
- curl_close($ch);
- return $ret_data;
- }
- }
|