laravel开发关于百度人脸识别及身份证认证

最近在做身份认证的时候用到了百度的身份证正反面上传识别及人脸识别

以下为文档

```
https://ai.baidu.com/ai-doc/FACE/skxie72kp#46-%E9%99%8D%E7%BA%A7%E6%B4%BB%E4%BD%93%E6%96%B9%E6%A1%88%E9%85%8D%E7%BD%AE
```

记住如果在开发中碰到任何未知的错误,不要硬撑,因为一般是因为权限不够

这个时候再花里胡哨都毫无意义

前端调用接口得到token,然后在自己代码一个百度的url加上这个代码 ,跳转到百度的页面完成身份证正反面上传及人脸识别,成功后跳转到指定的页面,然后在指定的页面上再调后台的接口`updateFaceRecognition`

```
//人脸识别及身份认证
private function getAccessToken(){
$access_token = Redis::get("access_token");
if(!$access_token){
$url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=".config('app.baidu_face_recognition_client_id')."&client_secret=".config('app.baidu_face_recognition_client_secret');
$content = Http::post($url);
$data = json_decode($content,true);
$access_token = $data['access_token'];
Redis::setex("access_token",60*60*24*30,$access_token);
}
return $access_token;
}

public function faceRecognition(){
$access_token = $this->getAccessToken();
$url = 'https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token='.$access_token;
$post_data = ["plan_id" => "15900"];
$content = Http::post($url,$post_data);
$data = json_decode($content,true);
$verify_token = $data['result']['verify_token'];
return $this->jsonData(['verify_token' => $verify_token]);
}

//成功接口
public function updateFaceRecognition(Request $request){
$ouser = $this->getOuserData();
$access_token = $this->getAccessToken();
$verify_token = $request->input("token");
$url = 'https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token='.$access_token;
$post_data = ["verify_token" => $verify_token];
$content = Http::post($url,$post_data);
$data = json_decode($content,true);
if($data['success'] == 1){
$res = AdminService::updateFaceRecognition($data,$ouser->id);
if(!$res){
return $this->jsonError(['data' => '认证失败']);
}
return $this->jsonData(['data' => '认证成功']);
}else{
return $this->jsonError(['data' => '认证失败']);
}
}
```

    A+
发布日期:2022年12月29日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: