身份證識別(極速數據接口對接)

  • 首先介紹一下主要實現識別身份證圖片,獲取到身份證圖片的信息。官方給的文檔很簡單,但是我折騰了很久,所以來總結一下,希望對大家有幫助。
  • 這是官方給的接口

官方地址: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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章