Aggregated data certificate identification interface - based on PHP sample code

1. Open the interface

The following code example is based on the certificate identification interface provided by aggregated data. Before using it, you need to register and apply for this interface to obtain the request key key.
Interface document address: https://www.juhe.cn/docs/api/id/153
insert image description here

2. Function introduction

Obtain the text content of regular certificates through automatic identification, eliminating the tedious manual input by users and ensuring the accuracy of the content.
According to the uploaded image file and certificate type, the certificate is recognized and the text content is returned.
Supports OCR recognition of more than 40 kinds of documents such as ID cards, bank cards, driver's licenses, and business licenses.

3. API documentation

Interface address: http://v.juhe.cn/certificates/typeList
Return format: json
Request method: get/post
Request example: http://v.juhe.cn/certificates/typeList?key=appkey
interface you applied for Remarks: Check the list of supported document types

4. Example code

<?php
// 证件识别接口 请求密钥key
$apiKey = '********';
// 证件识别接口的URL
$apiUrl = 'http://v.juhe.cn/certificates/query';

// 需要识别的图片信息
$imgFile = 'WechatIMG305.jpeg'; // 本地图片文件路径
$pic = curl_file_create($imgFile, 'image/jpeg', 'pic');

// 组装请求参数
$params = [
    'key' => $apiKey, // 您申请到的接口请求key
    'cardType' => 3, // 证件类型,依据支持的证件类型清单id修改
    'pic' => $pic
];

// 发起网络请求证件识别接口
$response = juheHttpRequest($apiUrl, $params, 1);
$result = json_decode($response, true);
if ($result) {
    
    
    print_r($result);
    // 请求成功,正常响应。 依据自己的业务逻辑修改
    $errorCode = $result['error_code'];
    if ($errorCode === 0) {
    
    
        // 识别成功
    } else {
    
    
        // 识别失败
    }
} else {
    
    
    // 可能网络异常等问题,无法正常获得相应内容,业务逻辑可自行修改
    echo "请求异常" . PHP_EOL;
}


/**
 * 发起网络请求函数
 * @param $url 请求的URL
 * @param bool $params 请求的参数内容
 * @param int $ispost 是否POST请求
 * @return bool|string 返回内容
 */
function juheHttpRequest($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_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));

    if ($ispost) {
    
    
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
    
    
        if ($params) {
    
    
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } 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;
}

3. Interface response result

{
    
    
    "reason":"操作成功",
    "result":{
    
    
        "保留":"",
        "签发机关":"苏州市公安局工业园区分局",
        "有效期限":"20160607-20360607",
        "签发日期":"2016-06-07",
        "有效期至":"2036-06-07"
    },
    "error_code":0
}

Guess you like

Origin blog.csdn.net/juhedata/article/details/130752344