广联达实名制系统对接源码

公司接了个广联达实名制对接的活,今天分享一下代码。代码为thinkphp框架开发,请家人们多多指教。

# 对接网址
	protected static $apiurl = 'https://glm.glodon.com';
// 	protected static $apiurl = 'https://glm-test.glodon.com/';
	

	# 请求封装成一个方法
	public static function postdata($appid, $secret, $projectCode,$body, $posturl)
	{
		$headers = [
            "Content-Type: application/json; charset=utf-8",
        ];
		# 查询数据库获取appid等信息
		# 时间戳
		$timestamp = date('YmdHis', time());
		# 随机字符
		$nonce = self::randstrpay();
		$strdata = json_encode($body);
		# 加密请求数据
		$aesdata = (new Aes())->gldencrypt($strdata,$secret,substr($secret,0,16));
		$data=[
            "appId" => $appid,
			"projectCode" => $projectCode,
			"nonce" => $nonce,
			"timestamp" => $timestamp,
			"sign" => self::sign($appid, $secret, $projectCode, $aesdata, $nonce, $timestamp),
			"data" => $aesdata
            ];
		
		$url = self::$apiurl.$posturl;
        // var_dump($data);
        //向该URL,发送Post请求
        $data = json_encode($data);
        // var_dump($data);
        $result = self::get_curl_request($url,$data,"POST" ,$headers);
        // var_dump($result);
        // sleep(10);
		return $result;
	}
    # 签名方法
	public static function sign($appid, $secret, $projectCode, $data, $nonce, $timestamp)
    {
		$string = $secret.'appId='.$appid.'&data='.$data.'&nonce='.$nonce.'&projectCode='.$projectCode.'&timestamp='.$timestamp.''.$secret;
		$str = md5(strtolower($string));
		return $str;
    }
	# 生成随机32位字符
   public static function randstrpay($length=32) {
       $rand='';
       $randstr= 'abcdefghijklmnopqrstuvwxyz0123456789';
       $max = strlen($randstr)-1;
       mt_srand((double)microtime()*1000000);
       for($i=0;$i<$length;$i++) {
           $rand.=$randstr[mt_rand(0,$max)];
       }
       return $rand;
   }

	# 请求方法重写
	public static function get_curl_request($url, $param = [], $mothod = 'POST', $headers = [], $return_status = 0, $flag = 0)
    {
        $ch = curl_init();
        if (!$flag) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }

		//内网需要开启代理
        //curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
        //curl_setopt($ch, CURLOPT_PROXYPORT, 12639);

        curl_setopt($ch, CURLOPT_TIMEOUT, 6);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if (strtolower($mothod) == 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        } else {
            $url = $url . "?" . http_build_query($param);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $ret  = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if ($return_status == "1") {
            return array($ret, $code);
        }
        return $ret;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨信

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值