TencentOcrHelper.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\DataApiNew\Helper;
  3. use Exception;
  4. use Illuminate\Support\Facades\Log;
  5. use TencentCloud\Common\Credential;
  6. use TencentCloud\Common\Profile\ClientProfile;
  7. use TencentCloud\Common\Profile\HttpProfile;
  8. use TencentCloud\Ocr\V20181119\OcrClient;
  9. use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
  10. use TencentCloud\Common\Exception\TencentCloudSDKException;
  11. define('SECRETID','AKIDfZYgknlegjrrgRKIC1nV8eDvmr28MfwH');
  12. define('SECRETKEY','cFyeDpMp9PXefjpt0782thQCvZ6Wr9tV');
  13. class TencentOcrHelper
  14. {
  15. // 身份证识别
  16. public function idCardCheck($imageUrl = "",$CardSide = 1)
  17. {
  18. try {
  19. // 初始化凭证
  20. $cred = new Credential(SECRETID, SECRETKEY);
  21. // 配置HTTP Profile
  22. $httpProfile = new HttpProfile();
  23. $httpProfile->setEndpoint("ocr.tencentcloudapi.com");
  24. // 配置Client Profile
  25. $clientProfile = new ClientProfile();
  26. $clientProfile->setHttpProfile($httpProfile);
  27. // 初始化OCR客户端
  28. $client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
  29. // 创建请求对象
  30. $req = new IDCardOCRRequest();
  31. if($CardSide == 1){
  32. $CardSide = "FRONT";
  33. }else{
  34. $CardSide = "BACK";
  35. }
  36. // 设置身份证图片的URL或Base64编码
  37. $params = array(
  38. "ImageUrl" => $imageUrl,
  39. "CardSide" => $CardSide, // FRONT表示身份证正面,BACK表示身份证反面
  40. );
  41. $req->fromJsonString(json_encode($params));
  42. // 发送请求并获取响应
  43. $resp = $client->IDCardOCR($req);
  44. // 输出结果
  45. return $resp->toJsonString();
  46. } catch (TencentCloudSDKException $e) {
  47. Log::error('身份证识别异常',[$e]);
  48. return false;
  49. } catch (Exception $e) {
  50. Log::error('身份证识别异常',[$e]);
  51. return false;
  52. }
  53. }
  54. }