- 首先介绍一下主要实现识别身份证图片,获取到身份证图片的信息。官方给的文档很简单,但是我折腾了很久,所以来总结一下,希望对大家有帮助。
- 这是官方给的接口
官方地址:https://www.jisuapi.com/api/idcardrecognition/
我用的是文档给的示例代码,但是有改动的地方,一定要注意,这个文档并没有介绍。
这个是官方封装的公共方法,请求时候会用到。(如果你能行,也可以自己封装。但是我为了方便就直接用它的了。)
public static function curlOpen($url, $config = array())
{
$arr = array('post' => false,
'referer' => $url,
'cookie' => '',
'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)',
'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),
'gzip'=>true,'ssl'=>false,'isupfile'=>false);
$arr = array_merge($arr, $config);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
//curl_setopt($ch, CURLOPT_HEADER, true);//获取header
if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if($arr['ssl'])
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if(!empty($arr['cookie']))
{
curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
}
if(!empty($arr['proxy']))
{
//curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
if(!empty($arr['userpwd']))
{
curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
}
}
//ip比较特殊,用键值表示
if(!empty($arr['header']['ip']))
{
array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
unset($arr['header']['ip']);
}
$arr['header'] = array_filter($arr['header']);
if(!empty($arr['header']))
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
}
if ($arr['post'] != false)
{
curl_setopt($ch, CURLOPT_POST, true);
if(is_array($arr['post']) && $arr['isupfile'] === false)
{
$post = http_build_query($arr['post']);
}
else
{
$post = $arr['post'];
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$result = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
return $result;
}
接下来就是封装身份证识别的方法了,(仔细阅读注释,希望对你有帮助)
public static function shiming2($typeid,$pic){
//$typeid=2身份证正面 3身份证反面
//$pic ,我用的是base64,不过前端传过来的如果‘,’逗号前有data:这些值要去掉,只保留‘/’以及‘/’以后的数据。
$appkey = '********';//你的appkey
$pic=substr($pic,strpos($pic,',')+1); //base64做处理
$url = "https://api.jisuapi.com/idcardrecognition/recognize?appkey=$appkey";
$post = array(
//'pic'=>curl_file_create('C:\Users\Public\Pictures\Sample Pictures\7777.jpg'), //'@'.realpath('11.jpg')
'pic'=>$pic, //如果用base64,就不用curl_file_create()方法。
'typeid'=>$typeid
);
$result = self::curlOpen($url, array('post'=>$post,'ssl'=>true));//base64也不用isupfile参数。记得加上ssl参数,这个文档没有提示。
//$result = self::curlOpen($url, array('post'=>$post,'ssl'=>true,'isupfile'=>true));//次行用作pic为本地地址时候使用。注意与base64的请求时参数的区别
$jsonarr = json_decode($result, true);
if($jsonarr['status'] != 0)
{
return ['code'=>1,'msg'=>$jsonarr['msg']];
}
$result = $jsonarr['result'];
//以下是我个人做的逻辑处理,你们要根据自己需求自己编写。
$data['code']=2;
$data['name']='';
$data['number']='';
if($typeid==2){
if(!empty($result['name'])&&!empty($result['number'])){
$data['name']=$result['name'];
$data['number']=$result['number'];
return $data;
}else{
return ['code'=>1,'msg'=>'身份证识别错误'];
}
}elseif($typeid==3){
if(!empty($result['enddate'])){
return $data;
}else{
//return $data;//不识别身份证背面背面
return ['code'=>1,'msg'=>'身份证识别错误'];
}
}else{
return ['code'=>1,'msg'=>'参数错误'];
}
}
返回的数据打印$jsonarr,正面
array(3) {
["status"]=>
int(0)
["msg"]=>
string(2) "ok"
["result"]=>
array(7) {
["address"]=>
string(42) "广东省新丰县丰城镇涧下村一组"
["birth"]=>
string(10) "1988-07-23"
["name"]=>
string(9) "罗淑誉"
["number"]=>
string(18) "440233198807233523"
["sex"]=>
string(3) "女"
["nation"]=>
string(3) "汉"
["retain"]=>
string(0) ""
}
}
反面:如果传入不是身份证背面的图片,识别出来ok的,只是没有获取到参数。(我想身份证正面也是一样的),所以后续判断参数status不建议作为成功的标准。所以严谨判断返回参数。
- 我误入的坑,关于$pic
- 开始$pic我传入的是地址,包括(网络地址,本地缓存地址)我传的地址链接都是可以访问到图片的,我以为这样就可以了。结果null;
- 问了客服他说是我电脑本地的地址(可能是我比较菜,我从没有接触过传递电脑本地地址的参数),然后就手动填入电脑本地图片的地址,结果还是null,
- 后来她说
self::curlOpen($url, array('post'=>$post,'isupfile'=>true));
这里要加'ssl'=>true,
-
self::curlOpen($url, array('post'=>$post,'ssl'=>true,'isupfile'=>true));
结果OK。
-
后来我改用base64传参,像这样:
结果 null
我猜这肯定是要该什么参数了。
1. 将‘,’逗号前面的数据去掉
2.$pic不用curl_file_creat()方法
'pic'=>$pic, //如果用base64,就不用curl_file_create()方法。
3.参数去掉'isupfile'=>true
$result = self::curlOpen($url, array('post'=>$post,'ssl'=>true));
结果OK
如果在对接中还有什么问题,可以加他们的技术群852057961