Ocr.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common;
  3. use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
  4. use HuaweiCloud\SDK\Core\Exceptions\{ConnectionException,RequestTimeoutException,ServiceResponseException};
  5. use HuaweiCloud\SDK\Core\Http\HttpConfig;
  6. use HuaweiCloud\SDK\Ocr\V1\Model\{BusinessLicenseRequestBody,InvoiceVerificationRequestBody,RecognizeBusinessLicenseRequest,RecognizeInvoiceVerificationRequest};
  7. use HuaweiCloud\SDK\Ocr\V1\OcrClient;
  8. class Ocr {
  9. private static $endpoint = 'https://ocr.cn-north-4.myhuaweicloud.com';
  10. private static $projectId = "f3818e89528d4b0db0c6353195653f04";
  11. private static $credentials;
  12. private static $instance;
  13. public function __construct() {
  14. $ak=env('huawei.ak');
  15. $sk=env('huawei.sk');
  16. self::$credentials = new BasicCredentials($ak,$sk,self::$projectId);
  17. }
  18. public static function getInstance() {
  19. if (null === self::$instance) {
  20. self::$instance = new self();
  21. }
  22. return self::$instance;
  23. }
  24. /**
  25. * @param string $url
  26. * @param string $image
  27. * @return array
  28. */
  29. public static function businessLicense($url='',$image='') {
  30. $client = self::OcrInfo();
  31. // 业务公司营业执照识别 为例
  32. if ($image!==""&&$url!=="") $url="";//图片数据流优先级最高。链接上传需要下载图片到识别服务器钟速度更慢
  33. $request = new RecognizeBusinessLicenseRequest();
  34. $body = new BusinessLicenseRequestBody();
  35. if($image!="")$body->setImage($image);
  36. if($url!="")$body->setUrl($url);
  37. $request->setBody($body);
  38. $msg = '';
  39. try {
  40. $response = $client->RecognizeBusinessLicense($request);
  41. } catch (ConnectionException $e) {
  42. $msg = $e->getMessage();
  43. } catch (RequestTimeoutException $e) {
  44. $msg = $e->getMessage();
  45. } catch (ServiceResponseException $e) {
  46. $msg = $e->getErrorMsg();
  47. }
  48. if ($msg) {
  49. return ["isResult"=>false,"msg"=>$msg];
  50. } else {
  51. return ['isResult'=>true,'msg'=>"获取成功","data"=>json_decode($response->getResult()->__toString(),true)];
  52. }
  53. }
  54. protected static function OcrInfo() {
  55. $config = HttpConfig::getDefaultConfig();
  56. $config->setIgnoreSslVerification(true);
  57. $client = OcrClient::newBuilder()
  58. ->withHttpConfig($config)
  59. ->withEndpoint(self::$endpoint)
  60. ->withCredentials(self::$credentials)
  61. ->build();
  62. return $client;
  63. }
  64. /**
  65. * Array of property to type mappings. Used for (de)serialization
  66. * code 发票代码。发票种类为全电发票时,该参数须为空字符串。
  67. * number 发票号码
  68. * issueDate 发票日期格式YYYY-MM-DD
  69. * checkCode 校验码后六位。 - 以下种类发票,参数不可为空 增值税普通发票、增值税电子普通发票、增值税普通发票(卷式)、增值税电子普通发票(通行费)、区块链电子发票。
  70. * - 区块链电子发票需要填写5位校验码。
  71. * subtotalAmount 合计金额。和票据上的金额的有效数字保持一致,例如票据上的金额为88.00,则需要输入字符串为“88.00”,才能验真成功。如果输入“88”或“88.0”可能会产生\"result_code\": \"1010\", \" Parameter error.\"报错。
  72. * 发票种类为全电发票时,该参数须为价税合计金额,其他票种使用不含税合计金额。 - 以下种类发票,参数不可为空 增值税专用发票、增值税电子专用发票、机动车销售统一发票、二手车销售统一发票、区块链电子发票、全电发票。 - 填写发票合计金额(不含税) 增值税专用发票、增值税电子专用发票、机动车销售统一发票、区块链电子发票。 - 二手车发票需要填写发票车价合计金额
  73. * @var string[]
  74. */
  75. public static function InvoiceVerification($code='',$number='',$issueDate='',$checkCode='',$subtotalAmount='') {
  76. $client = self::OcrInfo();
  77. $request=new RecognizeInvoiceVerificationRequest();
  78. $body = new InvoiceVerificationRequestBody();
  79. $issueDate = date('Y-m-d',strtotime($issueDate));
  80. if($checkCode!=""&&strlen($checkCode)!=6)$checkCode=substr($checkCode,0,6);
  81. if($subtotalAmount!="")$subtotalAmount= number_format($subtotalAmount, 2, '.', '');
  82. $body->setCode($code);
  83. $body->setNumber($number);
  84. $body->setIssueDate($issueDate);
  85. $body->setCheckCode($checkCode);
  86. $body->setSubtotalAmount($subtotalAmount);
  87. $request->setBody($body);
  88. $msg = '';
  89. try {
  90. $response = $client->RecognizeInvoiceVerification($request);
  91. }catch (ConnectionException $e) {
  92. $msg = $e->getMessage();
  93. } catch (RequestTimeoutException $e) {
  94. $msg = $e->getMessage();
  95. } catch (ServiceResponseException $e) {
  96. $msg = $e->getErrorMsg();
  97. }
  98. if ($msg) {
  99. return ["isResult"=>false,"msg"=>$msg];
  100. } else {
  101. return ['isResult'=>true,'msg'=>"获取成功","data"=>json_decode($response,true)];
  102. }
  103. }
  104. }