PHP 获取银行卡所属信息

PHP 简单获取银行卡所属信息
注册银联账号,申请开放接口相关使用配置信息,把自己的服务器ip添加到白名单
银联开发平台:https://open.unionpay.com/tjweb/login

getToken($APP_ID,$APP_SECRET);
        
        //调用的API
        $api_url = "https://openapi.unionpay.com/upapi/cardbin/cardinfo?token=$TOKEN";
        
        //注意注意!银联的java是用毫秒时间戳,
        $time =$this->msectime();
        
        //参数拼接
        $str = $signature.$body.$time;
        
        //sha256签名计算
        $sign =hash('sha256', $str);

        //请求链接拼接时间戳和签名
        $api_url = $api_url."&ts=$time&sign=$sign";

        //post请求传body报文
        $res = $this->httpPost($api_url,$body);
        
        var_dump($res);
    }

    //获取token
    public function getToken($appid,$appsecret){

        $url = "https://openapi.unionpay.com/upapi/cardbin/token?app_id=$appid&app_secret=$appsecret";
          
        $res = $this->httpGet($url);

        $res=json_decode($res,true);

        return $res['token'];
    }

    //获取当前毫秒时间戳
    public function msectime() {

        list($msec, $sec) = explode(' ', microtime());

        $time = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);

        return $time;

    }

    public function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    public function httpPost($url, $post_data){
        // header传送格式
        $headers = array(
            "content-type:application/json"
        );
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, false);
        //设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, true);
        // 设置post请求参数
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        // 添加头信息
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        // CURLINFO_HEADER_OUT选项可以拿到请求头信息
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        // 不验证SSL
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }
}

你可能感兴趣的:(PHP 获取银行卡所属信息)