84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Created by Vim
|
|
* User: lcc
|
|
* Desc: 阿里云图片识别
|
|
* Date: 2021-07-16
|
|
* Time: 16:49
|
|
*/
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
use AlibabaCloud\Client\AlibabaCloud;
|
|
use AlibabaCloud\Client\Exception\ClientException;
|
|
use AlibabaCloud\Client\Exception\ServerException;
|
|
class AliOrc{
|
|
|
|
private $accessKeyId;
|
|
private $accessKeySecret;
|
|
private $ci;
|
|
private $dir;
|
|
private $log_file = 'error.log';
|
|
public function __construct($accessKeyId='',$accessKeySecret=''){
|
|
if($accessKeyId && $accessKeySecret){
|
|
$this->accessKeyId = $accessKeyId;
|
|
$this->accessKeySecret = $accessKeySecret;
|
|
}else{
|
|
$this->accessKeyId = 'LTAI5t9utLGSVTtVTCw8Ts2k';
|
|
$this->accessKeySecret = 'U1dwYSkV1X42APP3IwqggyhemMfPtu';
|
|
}
|
|
$this->ci = & get_instance();
|
|
$this->dir = 'aliorc';
|
|
}
|
|
|
|
/**
|
|
* 身份证识别
|
|
* Desc: 阿里云官方地址:https://help.aliyun.com/document_detail/151899.htm?spm=a2c4g.11186623.2.7.31e276d9S794pF&accounttraceid=08df74bec8b54e9d8c4c3f8728cc714bppvo#doc-api-ocr-RecognizeIdentityCard
|
|
* @param $imageUrl string 身份证图片地址
|
|
* @param $side string 省份证正反面 (face 正面 back 反面)
|
|
* @param return array 返回值参考官方文档
|
|
*/
|
|
public function IdentityCard($imageUrl,$side='face'){
|
|
//dev测试
|
|
$this->ci->load->library('mycurl');
|
|
$debug_url = 'http://104.194.86.23:8888';
|
|
$params = [
|
|
'imageUrl' => $imageUrl,
|
|
'side' => $side
|
|
];
|
|
$result = $this->ci->mycurl->httpGet($debug_url,$params);
|
|
$result = json_decode($result,true);
|
|
return $result;
|
|
//end dev测试
|
|
AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
|
|
->regionId('cn-shanghai')
|
|
->asDefaultClient();
|
|
try {
|
|
$options = [
|
|
'query' => [
|
|
'ImageURL'=> $imageUrl,
|
|
'Side' => $side
|
|
]
|
|
];
|
|
$result = AlibabaCloud::rpc()
|
|
->product('ocr')
|
|
->scheme('http')
|
|
->version('2019-12-30')
|
|
->action('RecognizeIdentityCard')
|
|
->method('POST')
|
|
->host('ocr.cn-shanghai.aliyuncs.com')
|
|
->options($options)
|
|
->request();
|
|
$data = $result->toArray();
|
|
debug_log(json_encode($data,JSON_UNESCAPED_UNICODE),'success.log',$this->dir);
|
|
return ['code'=>1,'data'=>$data['Data']];
|
|
} catch (ClientException $e) {
|
|
debug_log($e->getErrorMessage,$this->log_file,$this->dir);
|
|
return ['code'=>0,'msg'=>$e->getErrorMessage];
|
|
} catch (ServerException $e) {
|
|
debug_log($e->getErrorMessage,$this->log_file,$this->dir);
|
|
return ['code'=>0,'msg'=>$e->getErrorMessage];
|
|
}
|
|
|
|
}
|
|
}
|