身份证ocr识别,解析姓名、性别、出生日期、身份证号。
下面是测试代码:
<?php
header('content-type:text/html charset:utf-8');
$img_base64='';
$file = "05.jpg";//图片地址
if($fp = fopen($file,"rb", 0)){
$gambar = fread($fp,filesize($file));
fclose($fp);
$base64 = chunk_split(base64_encode($gambar));
//输出
$img_base64=$base64;
}
//echo $img_base64;
$url='https://elife.icbc.com.cn/OFSTCARD/felicity/ocr.do';
$data='{"cardflag": "1","flag": "2","identity": "data:image/jpeg;base64,'.$img_base64.'","identityImageName": "11.jpg","imgkey": ""}';
$header=array(
"Referer: https://elife.icbc.com.cn",
"Sec-Fetch-Mode: cors",
"Sec-Fetch-Site: same-origin",
"Origin: https://elife.icbc.com.cn",
"Sec-Fetch-Dest: empty",
"Content-Type: application/json",
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
);
$result=https_request($url, $data, $header);
echo $result;
function https_request($url, $data = null, $header){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
if ( !empty($header) ) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
$output = curl_exec($curl);
curl_close($curl);
if($output){
return $output;
} else {
return curl_errno($curl);
}
}
运行结果:
{
"date": "1954-12-28",
"imgkey": "dc6694xxxxxxx35d0",
"sex": "男",
"name": "韦小宝",
"errorCode": "0",
"custCode": "11284419541228213X",
"errorMsg": "success"
}
图片无法识别返回:
{
"errorCode": "666031",
"errorMsg": "身份证识别结果校验失败!"
}