You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/common/library/baidu/OcrLicense.php

129 lines
3.8 KiB

9 months ago
<?php
namespace app\common\library\baidu;
class OcrLicense
{
const LICENSEURL = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license';
const TOKENURL = 'https://aip.baidubce.com/oauth/2.0/token';
private int $status = 1;
private string $msg = 'success';
private array $data = [];
private string $clientId;
private string $clientSecret;
public function __construct(string $clientId, string $clientSecret)
{
5 months ago
$this->clientId = trim($clientId);
$this->clientSecret = trim($clientSecret);
9 months ago
}
public function check($img_url): array
{
$token = $this->getToken();
if (!$token) {
return $this->resultHandle();
}
$data['url'] = $img_url;
$data['risk_warn'] = 'true';
$url = self::LICENSEURL . '?access_token=' . $token;
$result = $this->post($url, $data);
5 months ago
$this->msg = json_encode($result);
5 months ago
$this->validateData($result);
9 months ago
return $this->resultHandle();
}
private function validateData($data)
{
$name = '';
$credit_code = '';
if (!empty($data->log_id) && !empty($data->words_result) && !empty($data->words_result_num)) {
if (!empty($data->risk_type)) {
$this->riskType($data->risk_type);
if (!$this->status) {
return;
}
}
$words_result_num = intval($data->words_result_num);
if ($words_result_num != 16) {
$this->msg = '证件信息不全';
return;
}
//身份信息
$arr = $data->words_result;
if (!empty($arr->单位名称->words)) {
$name = $arr->单位名称->words;
}
if (!empty($arr->社会信用代码->words)) {
$credit_code = $arr->社会信用代码->words;
}
} else {
$this->msg = '接口异常';
}
$this->data = compact('name', 'credit_code');
}
private function riskType(string $key)
{
$this->status = 0;
switch ($key) {
case 'normal':
$this->status = 1;
break;
case 'copy':
$this->msg = '不能上传复印件';
break;
case 'temporary':
$this->msg = '不能上传临时证件';
break;
case'screen':
$this->msg = '不能上传翻拍证件';
break;
default:
"不合法的证件照";
break;
}
}
private function resultHandle(): array
{
return ['status' => $this->status, 'msg' => $this->msg, 'data' => $this->data];
}
private function post(string $url, array $postData)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
5 months ago
9 months ago
/**
* @notes:获取token
* @return mixed|string
* @author: wanghousheng
*/
private function getToken()
{
$token = '';
5 months ago
$data['grant_type'] = 'client_credentials';
$data['client_id'] = $this->clientId;
$data['client_secret'] = $this->clientSecret;
$result = $this->post(self::TOKENURL, $data);
9 months ago
if (!empty($result->access_token)) {
$token = $result->access_token;
} else {
5 months ago
$this->msg = '获取token失败';
9 months ago
}
return $token;
}
}