PHP

【PHP】获取接口所需的SignKey - 管家婆基础对接 PHP版本

小破孩
2025-04-23 / 0 评论 / 14 阅读 / 正在检测是否收录...
<?php


namespace app\common\lib\gjp;



use think\Exception;

class Basic
{
    protected $appKey;

    protected $SercretKey;

    protected $currentTimestamp;

    public function __construct()
    {
        $this->appKey = "*************";

        $this->SercretKey = "*************";

        $this->currentTimestamp = time();
    }

    public function getSignKey()
    {
        if(!empty( cache('gjp_sign')))
        {
            $dataResult = cache('gjp_sign');

            return $dataResult;
        }

        $signUrl = "http://api.cmgrasp.com/CMGraspApi/GateWay";

        $instanceStr = new \app\common\lib\data\Str();

        $randamStr = $instanceStr->setNonce(32,true);

        $md5BeforeStr = "AppKey".$this->appKey."InvalidTime".date('YmdHis',$this->currentTimestamp)."RandamStr".$randamStr.$this->SercretKey;

        $data = [
            "MethodName"  => (string)"graspcm.cmapi.getsignstr",
            'AppKey'      => (string)$this->appKey,
            'InvalidTime' => (string)date('Y-m-d H:i:s',$this->currentTimestamp),
            'RandamStr'   => (string)$randamStr,
            'SignStr'     => (string)md5($md5BeforeStr),
        ];

        $jsonData = json_encode($data);

        $result = $this->curlRequest($signUrl,"POST", $jsonData,true,false);

        if($result['RetCode'] != 0)
        {
            $this->getSignKey();
        }

        $dataResult = json_decode($result['RetMsg'],true);

        if(empty( cache('gjp_sign')))
        {
            $dataResult = json_decode($result['RetMsg'],true);

            cache('gjp_sign',$dataResult,60*60*20);
        }

        return $dataResult;
    }


    /**
     * @Author: 小破孩嫩
     * @Email: 3584685883@qq.com
     * @Time: 2021/4/1 10:39
     * @param string $url url地址
     * @param string $method 请求方法,默认为 'GET',可选值为 'GET' 或 'POST'
     * @param mixed $data 要发送的数据,如果是 POST 请求则为数据内容,否则为 null
     * @param array $headers 自定义请求头信息
     * @param int $timeout 超时时间,默认为 30 秒
     * @param bool $verifySSL 是否验证 SSL 证书,默认为 true
     * @param bool $flbg 返回值是否转成数组,默认不转
     * @param bool $headercontent 是否获取请求的header值内容,默认不获取
     * @return array|bool|mixed|string
     * @Description:curl请求
     */
    public function curlRequest($url, $method = 'GET', $data = null, $flbg = false, $verifySSL = true, $headers = [], $headerContent = false, $timeout = 30)
    {
        // 初始化 cURL 会话
        $ch = curl_init();

        // 设置要请求的 URL
        curl_setopt($ch, CURLOPT_URL, $url);
        // 设置获取的信息以字符串形式返回,而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 设置超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

        // 设置请求方法
        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        // 设置请求头
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        // 设置是否验证 SSL 证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifySSL);

        // 执行 cURL 会话并获取响应
        $response = curl_exec($ch);

        // 获取 HTTP 响应码
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        // 如果 cURL 执行出错
        if (curl_errno($ch)) {
            // 输出错误信息
            echo 'Curl error: ' . curl_error($ch);
            // 关闭 cURL 会话并返回 false
            curl_close($ch);
            return false;
        } // 如果 HTTP 响应码大于等于 400(表示错误)
        elseif ($httpCode >= 400) {
            // 输出错误信息
            echo "HTTP error: $httpCode";
            // 关闭 cURL 会话并返回 false
            curl_close($ch);
            return false;
        }

        // 处理是否获取请求头内容
        if ($headerContent && $httpCode == 200) {
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $headers = substr($response, 0, $headerSize);
            $body = substr($response, $headerSize);
            curl_close($ch);
            return [$headers, $body];
        }

        // 关闭 cURL 会话
        curl_close($ch);

        // 处理是否将响应转换为数组
        if ($flbg) {
            $response = json_decode($response, true);
        }

        // 返回响应内容
        return $response;
    }


}
0

评论 (0)

取消