123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\DataApiNew\Helper;
- use Exception;
- use Illuminate\Support\Facades\Log;
- use TencentCloud\Common\Credential;
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
- use TencentCloud\Ocr\V20181119\OcrClient;
- use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- define('SECRETID','AKIDfZYgknlegjrrgRKIC1nV8eDvmr28MfwH');
- define('SECRETKEY','cFyeDpMp9PXefjpt0782thQCvZ6Wr9tV');
- class TencentOcrHelper
- {
- // 身份证识别
- public function idCardCheck($imageUrl = "",$CardSide = 1)
- {
- try {
- // 初始化凭证
- $cred = new Credential(SECRETID, SECRETKEY);
-
- // 配置HTTP Profile
- $httpProfile = new HttpProfile();
- $httpProfile->setEndpoint("ocr.tencentcloudapi.com");
-
- // 配置Client Profile
- $clientProfile = new ClientProfile();
- $clientProfile->setHttpProfile($httpProfile);
-
- // 初始化OCR客户端
- $client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
-
- // 创建请求对象
- $req = new IDCardOCRRequest();
-
- if($CardSide == 1){
- $CardSide = "FRONT";
- }else{
- $CardSide = "BACK";
- }
-
- // 设置身份证图片的URL或Base64编码
- $params = array(
- "ImageUrl" => $imageUrl,
- "CardSide" => $CardSide, // FRONT表示身份证正面,BACK表示身份证反面
- );
- $req->fromJsonString(json_encode($params));
-
- // 发送请求并获取响应
- $resp = $client->IDCardOCR($req);
-
- // 输出结果
- return $resp->toJsonString();
- } catch (TencentCloudSDKException $e) {
- Log::error('身份证识别异常',[$e]);
- return false;
- } catch (Exception $e) {
- Log::error('身份证识别异常',[$e]);
- return false;
- }
- }
- }
|