| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- declare (strict_types=1);
- namespace app\traits;
- use think\facade\Log;
- /**
- * 请求公证处统一请求API
- * Trait GzcApiRequest
- * @package app\traits
- */
- trait GzcApiRequest
- {
- /**
- * 发送请求
- * @return mixed|string
- * @author php迷途小书童
- * @created 2021/1/14 14:34
- */
- public static function request(string $method, string $url, array $params = [], array $options = [])
- {
- $base_uri = env('GZC.HOSTNAME');
- $request_uri = $base_uri.$url;
- try {
- $data = json_encode($params);
- $headerArray =array(
- "Content-type:application/json;charset='utf-8'",
- "Accept:application/json"
- );
- if ($url != '/v1/api/third/auth/token') {
- $Token = self::getTokenFromServer();
- $accessToken = $Token['extendData']['accessToken'];
- array_push($headerArray, "token:".$accessToken);
- }
- Log::info($data);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $request_uri);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return json_decode($output, true);
- } catch (\Throwable $e) {
- return $e->getMessage();
- }
- }
- /**
- * 查询账户余额
- * @author php迷途小书童
- * @created 2021/1/14 16:34
- */
- public static function checkAmount($user)
- {
- $data = [
- 'platformNo' => env('GZC.PLATFORM_NO'),
- 'appNo' => env('GZC.APP_NO'),
- 'conName' => $user['user_name'],
- 'conCertType' => 1,
- 'conCertNo' => $user['user_card'],
- ];
- return self::request('POST', '/v1/api/third/in/in30', $data);
- }
- /**
- * 获取TOKEN
- * @return mixed|string
- * @author php迷途小书童
- * @created 2021/1/14 14:38
- */
- public static function getTokenFromServer()
- {
- $timestamp = (int)(microtime(true) * 1000);
- $appid = env('GZC.APP_ID');
- $data = [
- 'appid' => $appid,
- 'sign' => self::getSignContent($timestamp, $appid),
- 'timestamp' => $timestamp
- ];
- return self::request('POST', '/v1/api/third/auth/token', $data);
- }
- /**
- * 获取签名
- * @return string
- * @author php迷途小书童
- * @created 2020/10/29 9:35
- */
- public static function getSignContent(int $timestamp, string $appid) {
- $appsecret = md5(env('GZC.APP_SECRET'));
- $stringToBeSigned = "appid={$appid}&secret={$appsecret}×tamp={$timestamp}";
- return md5($stringToBeSigned);
- }
- }
|