//百度云鉴权获取 获取token
function baiduauth()
{
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$data = [
'grant_type'=>'client_credentials',
'client_id'=>Config::get('baidu.client_id'),
'client_secret'=>Config::get('baidu.client_secret'),
];
$rs = httpUtil($url,$data,'POST');
return $rs;
}
//身份证认证
public function code()
{
$img_url = Request()->param('img_url');//本地的身份证正面地址
$auth = json_decode($this->baiduauth(),true);
$token = $auth['access_token'];
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
$img = file_get_contents($img_url);
$img = base64_encode($img);
$bodys = array(
'id_card_side' => "front",
'image' => $img
);
$re = $this->request_post($url, $bodys);//json形成功获取的内容
$res = json_decode($re,true);//转为数组
//判断数据库有无该用户
if (
db('user_autonym')->where(['realname' => $res['words_result']['姓名']['words']])->count() >= 1
){
return $this->error('不能重复认证');
}
//给予错误信息
if($res['image_status']!='normal'){
switch ($res['image_status']){
case 'reversed_side';
return $this->error('身份证正反面颠倒');
break;
case 'non_idcard';
return $this->error('上传的图片中不包含身份证');
break;
case 'blurred';
return $this->error('身份证模糊');
break;
case 'other_type_card';
return $this->error('其他类型证照');
break;
case 'over_exposure';
return $this->error('身份证关键字段反光或过曝');
break;
case 'over_dark';
return $this->error('身份证欠曝(亮度过低)');
break;
case 'unknown';
return $this->error('未知状态');
break;
}
}
if($res['idcard_number_type']!=1){
switch ($res['idcard_number_type']){
case '-1';
return $this->error('身份证正面所有字段全为空');
break;
case '0';
return $this->error('身份证证号识别错误');
break;
case '2';
return $this->error('身份证证号和性别、出生信息都不一致');
break;
case '3';
return $this->error('身份证证号和出生信息不一致');
break;
case '4';
return $this->error('身份证证号和性别信息不一致');
break;
}
}
//如果没有该用户存到数据库
$data = [
'user_id' => $this->uid,
'realname' => $res['words_result']['姓名']['words'],
'id_card' => $res['words_result']['公民身份号码']['words'],
'jsoninfo' => $re,
'createtime' => time(),
'updatetime' => time(),
];
if (
db('user_autonym')->insert($data) == false
){
return $this->error('失败');
}
return $this->success('认证成功!');
}
//发起http post请求(REST API), 并获取REST请求的结果
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
成功后获取到的内容
{
"words_result":{
"姓名":{
"words":"姓名",
"location":{
"top":,
"left":,
"width":,
"height":
}
},
"民族":{
"words":"民族",
"location":{
"top":,
"left":,
"width":,
"height":
}
},
"住址":{
"words":"山东省xxxxx",
"location":{
"top":,
"left":,
"width":,
"height":
}
},
"公民身份号码":{
"words":"37xxxx",
"location":{
"top":,
"left":,
"width":,
"height":
}
},
"出生":{
"words":"19xxxx",
"location":{
"top":,
"left":,
"width":,
"height":
}
},
"性别":{
"words":"xxx",
"location":{
"top":,
"left":,
"width":,
"height":
}
}
},
"log_id":xx,
"words_result_num":xx,
"idcard_number_type":xx,
"image_status":"normal"
}