87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Created by Vim
|
|
* User: lcc
|
|
* Desc: 腾讯云图片识别
|
|
* Date: 2021-07-20
|
|
* Time: 16:49
|
|
*/
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
use TencentCloud\Common\Credential;
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
use TencentCloud\Ocr\V20181119\OcrClient;
|
|
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
|
|
class TcOrc{
|
|
|
|
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 = 'AKIDMXS7WhbEdlcCWZMs75mDNHuIPHsCF2Yn';
|
|
$this->accessKeySecret = 'zh8JDpAF3bOvCCvNYS5RXLs87pGQKxJO';
|
|
}
|
|
$this->ci = & get_instance();
|
|
$this->dir = 'tcorc';
|
|
}
|
|
|
|
/**
|
|
* 身份证识别
|
|
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/33524
|
|
* @param $imageUrl string 身份证图片地址
|
|
* @param $CardSide string 省份证正反面 (FRONT 正面 BACK 反面)
|
|
* @param return array 返回值参考官方文档
|
|
*/
|
|
public function IdentityCard($imageUrl,$CardSide='FRONT'){
|
|
//dev测试
|
|
$this->ci->load->library('mycurl');
|
|
$debug_url = 'http://104.194.86.23:8889/index.php';
|
|
$params = [
|
|
'ImageUrl' => $imageUrl,
|
|
'CardSide' => $CardSide
|
|
];
|
|
$result = $this->ci->mycurl->httpGet($debug_url,$params);
|
|
$result = json_decode($result,true);
|
|
return $result;
|
|
try {
|
|
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
|
|
$httpProfile = new HttpProfile();
|
|
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
|
|
|
|
$clientProfile = new ClientProfile();
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
|
|
|
|
$req = new IDCardOCRRequest();
|
|
|
|
$params = array(
|
|
"ImageUrl" => $imageUrl,
|
|
"CardSide" => $CardSide
|
|
);
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
$resp = $client->IDCardOCR($req);
|
|
|
|
debug_log($resp->toJsonString(),'success.log',$this->dir);
|
|
$result = json_decode($resp->toJsonString(),true);
|
|
if($result['Error']){
|
|
return ['code'=>0,'msg'=>$result['Error']['Message']];
|
|
}else{
|
|
return ['code'=>1,'msg'=>'识别成功','data'=>$result];
|
|
}
|
|
}
|
|
catch(TencentCloudSDKException $e) {
|
|
debug_log($e,$this->log_file,$this->dir);
|
|
return ['code'=>0,'msg'=>'识别失败'];
|
|
}
|
|
}
|
|
}
|