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 這個應該是參數不符合規則

很多人 +我問1012問題 這里做個補充,大概率是php版本問題這里用的是php7.3 加密部分的算法5與7是有區別的

還有就是測試接口得時候 要按照他文檔參數傳參 (每個接口對應的參數是不同的  是根據參數判斷是測試什么接口)  並且要注意他得接口測試的目的是什么 比如認證失敗 結果要失敗才能通過 

 希望文章能讓同學們少走點彎路

還有不懂可以加Q:876036823


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM