基于聚合数据的身份证实名认证API接口调用示例-PHP版 - sesen

时间:2024-03-10 14:06:30

本文转自:SDK.CN

一、申请接口

通过https://www.juhe.cn/docs/api/id/103自助申请开通接口,获取API请求KEY。

二、用示例代码PHP

//如需请求加密接口,加密方式请参考 https://www.sdk.cn/details/d591E8oY9X9r67veZz
$apiurl="http://op.juhe.cn/idcard/query";//请求地址
$key = "";//32位的KEY
$realname = "";//真实姓名
$idcard="";//身份证号码
$params=compact(\'key\',\'realname\',\'idcard\');//组合请求参数
$content=juhecurl($apiurl,$params);//获取接口返回内容json字符串
$result = json_decode($content,true);//解析成数组
if($result){
if($result[\'error_code\']==\'0\'){
if($result[\'result\'][\'res\'] == \'1\'){
echo "身份证号码和真实姓名一致";
}else{
echo "身份证号码和真实姓名不一致";
}
#print_r($result);
}else{
echo $result[\'error_code\'].":".$result[\'reason\'];
}
}else{
echo "请求失败";
}

//网络请求方法
function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();

curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 3);
curl_setopt( $ch, CURLOPT_TIMEOUT , 8);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if ($params) {
if (is_array($params)) {
$paramsString = http_build_query($params);
} else {
$paramsString = $params;
}
} else {
$paramsString = "";
}
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $paramsString);
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($paramsString ){
curl_setopt( $ch , CURLOPT_URL , $url.\'?\'.$paramsString);
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}