/*
* 请求身份验证接口
* $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位 不然也会报错
$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;
}