php网络游戏实名认证接口

相信大家最近都被防沉迷 接口整的头疼 也有很多坑 下面介绍下对接沉迷的注意事项与接口代码

1、首先在 接口测试 中获取到 APPID、Secret Key、bizId等测试数据  同时配置IP白名单 配置访问ip

2、直接上代码 ,需要用到的方法,自己写的 直接拿过去修改下可以用的

/* 
 * 请求身份验证接口
 * $name 姓名,$card 身份证号,$member_id 平台用户id,$test_code 测试码
 */
function getIdCard($name,$card,$member_id,$test_code='')
{
    $secret_key = '';   //正式secret_key
    $appId = '';        //正式appId 
    $bizId = '';        //正式bizId 
    $timestamps = getMillisecond();    //当前毫秒时间戳
    $url = 'https://api.wlc.nppa.gov.cn/idcard/authentication/check';
    if($test_code) {
        $secret_key = 'a84b4e7eaa4854cc7faf68285c4a61f7';    //测试secret_key
        $appId = '2dea4362b36f4b5d9ab19aef5801d72e';         //测试appId 
        $bizId = '1101999999';                               //测试bizId
        $url = 'https://wlc.nppa.gov.cn/test/authentication/check/'.$test_code;
    }
    $head = ['appId'=>$appId,'bizId'=>$bizId,'timestamps'=>$timestamps];
    //请求头
    $header[] = 'Content-Type:application/json;charset=utf-8';
    $header[] = 'appId:'.$appId;
    $header[] = 'bizId:'.$bizId;
    $header[] ='timestamps:'.$timestamps;
    //请求体
    $body['ai']    = md5($member_id);    //32位 不然也会报错 测试时 用文档提供得ai 不需要md5
    $body['name']  = $name;
    $body['idNum'] = $card;
    //请求体加密
    $data['data'] = bodyEncrypt($body,$secret_key);
    //请求头签名 一般报错1011是签名错误
    $header[]= 'sign:'.getSign($data,[],$secret_key,$head);
    //请求查询接口
    $res = reqCurl($url,3,'post',$data,$header);
    $res = json_decode($res,true);
    return $res;
}

/* 
 * 查询身份接口
 * $name 姓名,$card 身份证号,$member_id 平台用户id,$test_code 测试码
 */
function queryIdCard($name,$card,$member_id,$test_code='')
{
    $secret_key = '';
    $appId = '';
    $bizId = '';
    $timestamps = getMillisecond();
    $member_id  = md5($member_id);
    $url = 'https://api.wlc.nppa.gov.cn/idcard/authentication/query?ai='.$member_id;
    if($test_code) {
        $secret_key = 'a84b4e7eaa4854cc7faf68285c4a61f7';
        $appId = '2dea4362b36f4b5d9ab19aef5801d72e';
        $bizId = '1101999999';
        //查询接口是get请求 所以ai拼接到 url后面
        $url = 'https://wlc.nppa.gov.cn/test/authentication/query/'.$test_code.'?ai='.$member_id;    
    }
    //get请求携带ai 所以签名得时候ai也要参与 不然会1011
    $head = ['ai'=>$member_id,'appId'=>$appId,'bizId'=>$bizId,'timestamps'=>$timestamps];
    $header[] = 'Content-Type:application/json;charset=utf-8';
    $header[] = 'appId:'.$appId;
    $header[] = 'bizId:'.$bizId;
    $header[] ='timestamps:'.$timestamps;
    $body['ai']    = $member_id;
    $body['name']  = $name;
    $body['idNum'] = $card;
    $header[]= 'sign:'.getSign('',[],$secret_key,$head);
    //请求查询接口
    $res = reqCurl($url,3,'get',[],$header);
    $res = json_decode($res,true);
    return $res ;
}

/* 
 * 查询身份接口
 * $member_id 平台用户id,pi 身份接口返回得pi,bt/ct 如文档,$test_code 测试码
 */
function queryLoginout($member_id,$pi,$bt=0,$ct=0,$test_code='')
{
    $secret_key = '';
    $appId = '';
    $bizId = '';
    $timestamps = getMillisecond();
    $time = time();
    $url = 'http://api2.wlc.nppa.gov.cn/behavior/collection/loginout';
    if($test_code) {
        $secret_key = 'a84b4e7eaa4854cc7faf68285c4a61f7';
        $appId = '2dea4362b36f4b5d9ab19aef5801d72e';
        $bizId = '1101999999';
        $url = 'https://wlc.nppa.gov.cn/test/collection/loginout/'.$test_code;
    }
    $head = ['appId'=>$appId,'bizId'=>$bizId,'timestamps'=>$timestamps];
    $header[] = 'Content-Type:application/json;charset=utf-8';
    $header[] = 'appId:'.$appId;
    $header[] = 'bizId:'.$bizId;
    $header[] ='timestamps:'.$timestamps;
    //这里值得注意 没有collections 也会1011
    $body['collections'][] = ['no'=>1,'si'=>$member_id,'bt'=>$bt,'ot'=>$time,'ct'=>$ct,'pi'=>$pi];
    $data['data'] = bodyEncrypt($body,$secret_key);
    $header[]= 'sign:'.getSign($data,[],$secret_key,$head);
    //请求查询接口
    $res = reqCurl($url,3,'post',$data,$header);
    $res = json_decode($res,true);
    return $res;
}

//返回当前的毫秒时间戳
function getMillisecond()
{  
    list($t1, $t2) = explode(' ', microtime());  
    return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);  
}  

/**
 * 对请求报文体进行加密
 * @param $body
 * @return string
 */
function bodyEncrypt($body,$secret_key)
{
    $key = hex2bin($secret_key);
    $cipher = "aes-128-gcm";
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $encrypt = openssl_encrypt(json_encode($body), $cipher, $key, OPENSSL_RAW_DATA,$iv,$tag);
    return base64_encode(($iv.$encrypt.$tag));
}

/**
 * 接口签名
 * @param $body
 * @param $query_params
 */
function getSign($body_data='',$query_params,$secret_key,$header)
    {
        if(!empty($body_data)) {
            $encrypted_body = json_encode($body_data);
        } else {
            $encrypted_body = '';
        }
        $sys_params = $header;
        $final_params = array_merge($sys_params, $query_params);
        ksort($final_params);

        $str_params = '';
        foreach ($final_params as $k => $v) {
            $str_params .= $k.$v;
        }
        $str_params .= $encrypted_body;
        $str_params = $secret_key.$str_params;
        $hash = hash('sha256', $str_params);

        return $hash;
    }

//自定义curl
function reqCurl($url,$timeout = 3,$method="get",$body = [],$header=['Content-Type: application/json;charset=utf-8']){  
    $ret = "";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    // 从证书中检查SSL加密算法是否存在(默认不需要验证)
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($method == "post"){
        curl_setopt($ch, CURLOPT_POST, 1);        
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    }

    $buffer = curl_exec($ch);    

    curl_close($ch);   
    if ($buffer === false || empty($buffer)) {
        $ret = "";
    } else {
        $ret = $buffer;
    }

    return $ret;
}  

  

3、一些坑的注意

在  测试接口  下面还有一个文档 用于测试 这些测试接口得,不要想直接正式测 ,必须测试全部通过 才能正式使用 不然正式得回报1008

如果报错 1011 大概率是你的sign错误  或者你参数问题影响你得签名  我就犯了一个错误,用加密函数生成 请求体 然后在sign签名得时候再次调用加密函数 最后报错1011

如数据上报接口没有加上collections参数

查询接口没有携带业务参数ai

header格式不正确

明文信息需要加上"collections"参数,格式如:{"collections":[{"no":XXX,"si":"XXX",等等等}]} 请根据文档

其他像 1004 1005 文档上有原因 主要是1011 与1008 这两个错误比较难理解 还有一个1001 这个应该是参数不符合规则

还有就是测试接口得时候 要按照他文档参数传参 (每个接口对应的参数是不同的  是根据参数判断是测试什么接口)  并且要注意他得接口测试的目的是什么 比如认证失败 结果要失败才能通过 

还有不懂可以加Q:876036823

原文地址:https://www.cnblogs.com/emmmmmm/p/14785276.html