发票相关类.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\util\SysSDK\Baidu;
  3. use think\facade\Log;
  4. class Identify {
  5. private $vi_url = Config::get('baidu.request.vi_url'); //百度vi_url地址
  6. private $viv_url = Config::get('baidu.request.viv_url');//百度viv_url地址
  7. private $url = Config::get('baidu.request.oauth_url');//百度鉴权地址
  8. private $client_id = Config::get('baidu.request.client_id');//百度鉴权client_id
  9. private $client_secret = Config::get('baidu.request.client_secret');//百度鉴权client_id对应secret
  10. //重新获取鉴权认证
  11. public function refresh(){
  12. $post_data['grant_type'] = 'client_credentials';
  13. $post_data['client_id'] = $this->client_id;
  14. $post_data['client_secret'] = $this->client_secret;
  15. $o = "";
  16. foreach ( $post_data as $k => $v )
  17. {
  18. $o.= "$k=" . urlencode( $v ). "&" ;
  19. }
  20. $post_data = substr($o,0,-1);
  21. $data = $this->curl($this->url, $post_data);
  22. Log::info(' get access_token result:'.json_encode($data));
  23. return $data;
  24. }
  25. /*
  26. * 发票识别
  27. * access_token 百度 access_token 鉴权返回值
  28. * files 发票图片文件
  29. */
  30. public function vat_invoice($access_token, $files){
  31. $file_name_array = explode('.', $files);
  32. if(count($file_name_array) < 2){
  33. return false;
  34. }
  35. $suffix = $file_name_array[count($file_name_array)-1];
  36. //判断文件后缀
  37. if(in_array(strtolower($suffix), ['jpg','jpeg','png','bmp'])){
  38. $param_type = 'image';
  39. }
  40. else if(strtolower($suffix) == 'pdf'){
  41. $param_type = 'pdf_file';
  42. }
  43. else{
  44. return false;
  45. }
  46. $img = file_get_contents($files);
  47. $img = base64_encode($img);
  48. $param = array(
  49. $param_type => $img
  50. );
  51. //这里也可以自己写curl方法调用
  52. $data = $this->curl($this->vi_url.'?access_token='.$access_token, $param);
  53. Log::info(' get result:'.json_encode($data));
  54. return $data;
  55. }
  56. //发票验真
  57. //access_token 百度 access_token 鉴权返回值
  58. public function vat_invoice_verification($access_token, $invoice_code, $invoice_num, $invoice_date, $invoice_type, $check_code, $total_amount){
  59. $bodys = array(
  60. 'invoice_code' => $invoice_code,//"发票代码",
  61. 'invoice_num' => $invoice_num,//"发票号码",
  62. 'invoice_date' => $invoice_date,//"开票日期",
  63. 'check_code' => $check_code,//"校验码。填写发票校验码后6位",
  64. 'invoice_type' => $invoice_type,//"发票类型",
  65. 'total_amount' => $total_amount,//"不含税金额"
  66. );
  67. $data = $this->curl($this->viv_url.'?access_token='.$access_token, $bodys);
  68. Log::info(' get result:'.json_encode($data));
  69. return $data;
  70. }
  71. private function curl($url = '', $param = '')
  72. {
  73. if (empty($url) || empty($param)) {
  74. return false;
  75. }
  76. $postUrl = $url;
  77. $curlPost = $param;
  78. $curl = curl_init();//初始化curl
  79. curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
  80. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  81. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  82. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  83. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  84. $data = curl_exec($curl);//运行curl
  85. //记录每次获取到的信息
  86. Log::info(' get curl result:' . json_encode($data));
  87. curl_close($curl);
  88. return $data;
  89. }
  90. }