天天看點

身份證号碼API 查詢接口

作者:IT智能化專欄

接口描述

功能描述:由份證号碼查詢擷取身份證的出生日期、性别、簽發地區等資訊。

URL 示例

1)http 協定:

POST 方式請求:http://cha.ebaitian.cn/api/json?appid=xxx&module=getIDCardInfo&idcard=xxx&sign=xxx

GET 方式請求:http://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getIDCardInfo&idcard=xxx&sign=xxx

2)https 協定:

POST 方式請求:https://cha.ebaitian.cn/api/json?appid=xxx&module=getIDCardInfo&idcard=xxx&sign=xxx

GET 方式請求:https://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getIDCardInfo&idcard=xxx&sign=xxx

請求參數

資料包體

{
"type": "get",
"appid": "1000xxxx",
"module": "getIDCardInfo",
"idcard": "420101199001010000",
"sign": "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4"
}           

參數說明

參數必選類型描述

type否string授權接口的請求方式

appid是string授權接口的 AppID,請填寫您在我要查官網上申請到的 AppID

module是string目标請求的資料子產品,查詢身份證号碼為:getIDCardInfo

idcard是string目标要查詢的身份證号碼,僅支援18位二代身份證号碼

sign是string請求憑證,具體計算方式見下面的其他說明

其他說明

1)type:可選值 get,如果指派 get,則以 get 方式送出資料;預設以 post 方式送出資料;

2)sign:簽名校驗,根據公式 $sign=sha256(appid=$appid&module=getIDCardInfo&idcard=$idcard&appkey=$appkey) 生成;其中:appkey 為授權接口的 AppKey,請填寫您在我要查官網上申請到的 AppKey 。

構造僞代碼如下:

string type = "get"; //請求方式,可以指派為:post
string appid = "1000xxxx"; //sdkappid 對應的 appid,需要業務方高度保密
string module = "getIDCardInfo"; //請求的資料子產品,此處指派:getIDCardInfo
string idcard = "420101199001010000"; //要查詢的身份證号碼,注意僅支援18位二代身份證号碼
string sign = sha256(appid=1000xxxx&module=getIDCardInfo&idcard=420101199001010000&appkey=56cf61af4b7897e704f67deb88ae8f24);           

響應參數

資料包體

{
"result":1,
"description":"TRUE",
"flag":"",
"idcardInfo":{
"birthday":"1996年02月01日",
"sex":"女",
"province":"湖北省",
"city":"武漢市",
"dis":"東西湖區",
"note":null
}
}           

參數說明

參數必選類型描述

result是string接口響應結果:0-失敗;1-成功

description是string接口響應描述:一般為 TURE(result=1) 與 FALSE(result=0),或者傳回錯誤資訊

flag否string錯誤說明,沒有錯誤則傳回空

idcardInfo是object傳回身份證資訊

idcardInfo 參數說明:

參數必選類型描述

birthday是string出生日期

sex是string性别

province是string發證地區,省(市/自治區)

city是string發證地區,市(區/自治州)

dis是string發證地區,區(縣/市/區)

note否string其他備注資訊,一般為空

SDK 及代碼示例

PHP SDK

方法一:以 POST 方式請求資料

//接口參數
$api_url='http://cha.ebaitian.cn/api/json';
$api_appid='1000xxxx';
$api_appkey='56cf61af4b7897e704f67deb88ae8f24';
//函數,以POST方式送出資料,PHP需要開啟CURL函數;資料傳輸安全,建議使用
function getIDCardInfo($idcard){
global $api_url,$api_appid,$api_appkey;
$posturl=$api_url;
$data='appid='.$api_appid.'&module=getIDCardInfo&idcard='.$idcard;
$sign=hash("sha256",$data.'&appkey='.$api_appkey);
$postdata=array("appid"=>$api_appid,"appkey"=>$api_appkey,"module"=>"getIDCardInfo","idcard"=>$idcard,'sign'=>$sign);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $posturl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($curl);
curl_close($curl);
$obj=json_decode($output);
$result=$obj->result;
if($result==1){
$value=$obj->idcardInfo->birthday;
$value.=','.$obj->idcardInfo->sex;
$value.=','.$obj->idcardInfo->province;
$value.=','.$obj->idcardInfo->city;
$value.=','.$obj->idcardInfo->dis;
}else{
$value=$obj->flag;
}
return $value;
}
//調用函數
$idcard='420101199001010000';
echo getIDCardInfo($idcard);
exit;           

方法二:以 GET 方式請求資料

//接口參數
$api_url='http://cha.ebaitian.cn/api/json';
$api_appid='1000xxxx';
$api_appkey='56cf61af4b7897e704f67deb88ae8f24';
//函數,以GET方式送出資料
function getIDCardInfo($idcard){
global $api_url,$api_appid,$api_appkey;
$data='appid='.$api_appid.'&module=getIDCardInfo&idcard='.$idcard;
$sign=hash("sha256",$data.'&appkey='.$api_appkey);
$info_get=file_get_contents($api_url.'?type=get&'.$data.'&sign='.$sign);
$info_json=json_decode($info_get, true);
$result=$info_json['result'];
if($result==1){
$value=$info_json['idcardInfo']['birthday'];
$value.=','.$info_json['idcardInfo']['sex'];
$value.=','.$info_json['idcardInfo']['province'];
$value.=','.$info_json['idcardInfo']['city'];
$value.=','.$info_json['idcardInfo']['dis'];
}else{
$value=$info_json['flag'];
}
return $value;
}
//調用函數
$idcard='420101199001010000';
echo getIDCardInfo($idcard);
exit;           

Java SDK

//以下示例是以 GET 方式請求資料
public class QueryHelper {
public static String apiurl="http://cha.ebaitian.cn/api/json";
public static String appid="1000xxxx";
public static String appkey="56cf61af4b7897e704f67deb88ae8f24";
public static String module="getIDCardInfo";
public static String getSHA256Str(String str){
MessageDigest messageDigest;
String encdeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
encdeStr = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encdeStr;
}
public static String get(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
for (String s = br.readLine(); s != null; s = br.readLine()) {
builder.append(s);
}
br.close();
return builder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String queryIDCard(String idcard){
String sign=getSHA256Str("appid="+appid+"&module="+module+"&idcard="+idcard+"&appkey="+appkey);
String url=apiurl+"?type=get&appid="+appid+"&module="+module+"&idcard="+idcard+"&sign="+sign;
return get(url);
}
}
//使用示例
QueryHelper.queryIDCard("420101199001010000");           

Python SDK

#!/usr/bin/python
# -*- coding: utf-8 -*-
import httplib2
import hashlib
from urllib.parse import urlencode #python3
#from urllib import urlencode #python2
apiurl='http://cha.ebaitian.cn/api/json'
appid='1000xxxx'
appkey='56cf61af4b7897e704f67deb88ae8f24'
module='getIDCardInfo'
idcard='420101199001010000'
data='appid='+appid+'&module='+module+'&idcard='+idcard
sign_data=data+'&appkey='+appkey
# from Crypto.Cipher import AES
# from Crypto.Hash import SHA256
# 256
hash_256 = hashlib.sha256()
hash_256.update(sign_data.encode('utf-8'))
sign = hash_256.hexdigest()
postdata = urlencode({'appid':appid,'module':module,'idcard':idcard,'sign':sign})
url = apiurl+'?'+postdata
http = httplib2.Http()
response, content = http.request(url,'GET')
print(content.decode("utf-8"))
Node.js SDK           

方法一:以 POST 方式請求資料

//以 POST 方式送出
var http = require('http');
var querystring = require('querystring');
//參數設定
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getIDCardInfo';
//目标查詢身份證号碼
var idcard='420101199001010000';
//簽名,SHA256 不可直接調用;函數參考下載下傳位址:
https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&idcard='+idcard+'&appkey='+appkey);
//這是需要送出的資料
var post_data = {
appid: appid,
module: module,
idcard: idcard,
sign: sign
};
var content = querystring.stringify(post_data);
var options = {
hostname: 'cha.ebaitian.cn',
port: 80,
path: '/api/json',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
//JSON.parse(chunk)
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();           

方法二:以 GET 方式請求資料

//以 GET 方式送出
var http = require('http');
var querystring = require('querystring');
//參數設定
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getIDCardInfo';
//目标查詢身份證号碼
var idcard='420101199001010000';
//簽名,SHA256 不可直接調用;函數參考下載下傳位址:
https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&idcard='+idcard+'&appkey='+appkey);
//這是需要送出的資料
var data = {
appid: appid,
module: module,
idcard: idcard,
sign: sign
};
var content = querystring.stringify(data);
var options = {
hostname: 'cha.ebaitian.cn',
port: 80,
path: '/api/json?' + content,
method: 'GET'
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();           

C# SDK

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Text;
public class getIDCardInfo{
public static string getInfo(string appid, string appkey, string module, string idcard){
string url = string.Format("http://cha.ebaitian.cn/api/json?type=get&appid={0}&module={1}&idcard={2}&sgin={3}", appid, module, idcard, sgin);
using (WebClient client = new WebClient()){
client.Encoding = Encoding.UTF8;
return client.DownloadString(url);
}
}
}
string idcardInfo = getIDCardInfo.getInfo("1000xxxx", "getIDCardInfo", "420101199001010000", "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4", Request.UserHostAddress);
Console.WriteLine(idcardInfo);
Response.Write(idcardInfo);           

JavaScript SDK

方法一:以 POST 方式請求資料
//使用 JQuery 請先加載最新的 JQuery 插件
//參數設定
var apiurl = 'http://cha.ebaitian.cn/api/json';
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getIDCardInfo';
//目标查詢身份證号碼
var idcard='420101199001010000';
//簽名,SHA256 不可直接調用;函數參考下載下傳位址:
https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&idcard='+idcard+'&appkey='+appkey);
//送出資料
$.ajax({
url:apiurl,
type:'post',
dataType:'json',
data:{
appid:appid,
module:module,
idcard:idcard,
sign:sign
},
success:function(res){
console.log(res);
}
});           

方法二:以 GET 方式請求資料

//使用 JQuery 請先加載最新的 JQuery 插件
//參數設定
var apiurl = 'http://cha.ebaitian.cn/api/json';
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getIDCardInfo';
//目标查詢身份證号碼
var idcard='420101199001010000';
//簽名,SHA256 不可直接調用;函數參考下載下傳位址:
https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&idcard='+idcard+'&appkey='+appkey);
//送出資料
$.ajax({
url:apiurl,
type:'post',
dataType:'json',
data:{
appid:appid,
module:module,
idcard:idcard,
sign:sign
},
success:function(res){
console.log(res);
}
});           

ASP SDK

'設定參數
dim apiurl, appid, appkey, module, idcard, sign
apiurl="http://cha.ebaitian.cn/api/json"
appid="1000xxxx'
appkey="56cf61af4b7897e704f67deb88ae8f24"
module="getIDCardInfo"
idcard="420101199001010000"
'簽名,SHA256 不可直接調用;函數參考位址:
https://blog.csdn.net/yesoce/article/details/128546
sgin=SHA256("appid=&appid&"&module="&module&"&idcard="&idcard&"&appkey="&appkey)
'異步送出資料
function PostHTTPPage(url,data)
dim Http
set Http=server.createobject("MSXML2.SERVERXMLHTTP.3.0")
Http.open "POST",url,false
Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Http.send(data)
if Http.readystate<>4 then
exit function
End if
PostHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8")
set http=nothing
if err.number<>0 then err.Clear
End function
'送出資料
dim postdata, strTest
postdata="appid=&appid&"&module="&module&"&idcard="&idcard&"&sign="&sign
strTest=PostHTTPPage(apiurl,postdata)
'傳回結果
response.write(strTest)
response.end           

常見問題

API 接口參數為空

此錯誤傳回 JSON 資料如下:

Copy

{

"result":0,

"description":"API接口參數為空",

"flag":"appid:sign"

}

解決方法:

1)請檢查 appid 及 sign 是否為空;

2)確定 appid 是從官網擷取到正确的接口授權;

3)確定 sign 計算生成是正确的。

API 接口參數無效

此錯誤傳回 JSON 資料如下:

Copy

{

"result":0,

"description":"API接口參數無效",

"flag":"appid"

}

解決方法:

1)請檢查 appid 是否正确;

2)確定 appid 是從官網擷取到正确的接口授權。

API 接口授權已到期

此錯誤傳回 JSON 資料如下:

Copy

{

"result":0,

"description":"API接口授權已到期",

"flag":"end:2018-12-31 23:59:59"

}

解決方法:

1)請檢查 appid 對應接口授權的期限是否過期;

2)如果接口授權過期,請到官網更新(免費使用者直接更新,無需續費)或續費(針對商業付費使用者)。

簽名錯誤

此錯誤傳回 JSON 資料如下:

Copy

{

"result":0,

"description":"簽名錯誤",

"flag":"getIDCardInfo->sign"

}

解決方法:

1)請檢查 sign 簽名計算是否正确;

2)簽名 sign 根據公式 $sign=sha256(appid=$appid&module=getIDCardInfo&idcard=$idcard&appkey=$appkey) 生成;其中:appkey 為授權接口的 AppKey,請填寫您在我要查官網上申請到的 AppKey 。

請求受限

此錯誤傳回 JSON 資料如下:

Copy

{

"result":0,

"description":"請求受限",

"flag":"getIDCardInfo->daylimit"

}

解決方法:

1)授權接口已超出目前接口産品請求的最大限制;

2)請根據實際使用需求更新您的接口産品。