Home >Backend Development >PHP Problem >How to implement ID card OCR recognition in PHP

How to implement ID card OCR recognition in PHP

青灯夜游
青灯夜游Original
2022-10-21 18:22:282488browse

Methods to implement ID card OCR recognition: 1. Apply to open the ID card OCR recognition interface and obtain the API request KEY; 2. Call the interface API through "function juhecurl($url,$params=false,$ispost =0){...}", process the data and return the result; 3. Get the content returned by the interface and parse it into an array using json_decode(); 4. Print the return result.

How to implement ID card OCR recognition in PHP

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Identity based on PHP Certificate OCR recognition interface calling example

Application interface

Interface remarks: Some keys to identifying the front or back of the ID card Zi

Apply for self-service activation interface throughhttps://www.juhe.cn/docs/api/id/287?s=cpphpcn and obtain the API request KEY

Request parameters

Name Required Description
key is In Personal Center->My Data, view above the interface name
image is image data, base64 encoding (excluding data:image/jpeg;base64,), the size after base64 encoding is required not to exceed 4M, the shortest side is at least 15px, the minimum The maximum long side is 4096px, supporting jpg/png/bmp format
side is front: front recognition; back: reverse recognition;

PHP sample code

Please refer to https: //www.sdk.cn/details/vOVl36qOLmlV8E9WXJ

$apiurl="http://apis.juhe.cn/idimage/verify";//请求地址
$key = "";//32位的KEY
$image= "";//图片base64后字符串
$side="front";//正反面
$params=compact('key','image','side');//组合请求参数
$content=juhecurl($apiurl,$params,1);//post请求获取接口返回内容json字符串
$result = json_decode($content,true);//解析成数组
if($result){
    if($result['error_code']=='0'){
        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;
}

JSON return example

身份证正面识别结果示例:
{
"reason": "成功",
"result":{
         "realname": "张三",/*姓名*/
         "sex": "男",/*性别*/
         "nation": "侗",/*民族*/
         "born": "19760613",/*出生日期*/
         "address": "贵州省都匀市甘塘镇长红机器厂散居户169号",/*地址*/
         "idcard": "522701197606131935",/*身份证号*/
         "side": "front",/*正面*/
         "orderid": 339057896/*本次查询流水号*/
        

},
"error_code": 0
}
--------------------------------------------------------------------------------------------
身份证背面识别结果示例:
{
"reason": "成功",
"result":{
     "begin": "20130501",/*签发日期*/
     "department": "东台市公安局",/*签发机关*/
     "end": "20180501",/*失效日期*/ 
     "side": "back",/*反面*/
     "orderid": 478799279/*本次查询流水号*/
          },
"error_code": 0
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement ID card OCR recognition in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn