随着近年来移动互联网的迅速发展,网络安全和信息安全的重要性也日益凸显,各行业对实名认证的要求也越来越严格,身份证OCR识别接口也就应运而生,它可以让用户通过手机摄像头扫描身份证或者上传身份证图像,快速识别并自动填写相关信息,大大提升了用户体验。
以下是身份证OCR 识别 API 接口用如何PHP调用的示例代码:
php
<?php
$appKey = "您的appKey";
$appSecret= "您的appSecret";
$url = "https://api.shuxuntech.com/v1/idCard/ocr";
$method = "POST";
$timestamp = getUnixTimestamp();
var_dump($timestamp);
$sign = hash('sha256',$appKey . $timestamp . $appSecret);
var_dump($sign);
$headers = array();
array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
array_push($headers, "appKey" . ":" . appKey);
array_push($headers, "timestamp" . ":" . timestamp);
array_push($headers, "sign" . ":" . sign);
$image = "";
$imgUrl = "";
$bodys = "image=" . $image . "&imgUrl=" . $imgUrl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$".$url, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
var_dump(curl_exec($curl));
function getUnixTimestamp () {
list($s1, $s2) = explode(' ', microtime());
return sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
}
返回的数据:
(1)正面返回
php
{
"code": "0",
"msg": "成功",
"isFee": 1,
"seqNo": "0harl9qk12a2cav0mmy25uufl3df4uid",
"data": {
"result": 1,
"resultDesc": "识别成功,有数据",
"info": {
"side": "front",
"type": 1,
"front": {
"birthday": "1990-01-21",
"address": "杭州市余杭区仓前街道某某小区1幢801室",
"gender": "男",
"race": "汉",
"name": "张三",
"idCard": "330110199001212311"
},
"back": null
}
}
}
(2)反面返回
php
{
"code": "0",
"msg": "成功",
"isFee": 1,
"seqNo": "n2d8oo1qzxhgf9t399lairvfflztkh49",
"data": {
"result": 1,
"resultDesc": "识别成功,有数据",
"info": {
"side": "back",
"type": 1,
"front": null,
"back": {
"issuedBy": "杭州市公安局余杭分局",
"validDate": "2021.06.21-2041.06.21"
}
}
}
}
(3)错误返回
php
{
"code": "1",
"msg": "参数错误",
"isFee": 0,
"seqNo": null,
"data": null
}
需要特别注意的是,在使用身份证OCR 识别 API 之前,我们首先需要申请并获取 API Key,不同的平台获取 API Key 的方式可能会有所不同。但是这个 API Key 将作为我们身份验证的凭证,在后续的接口请求中必须携带。