| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | <?phpnamespace app\common\util\SysSDK\Baidu;use think\facade\Log;class Identify {		private $vi_url = Config::get('baidu.request.vi_url'); //百度vi_url地址	private $viv_url = Config::get('baidu.request.viv_url');//百度viv_url地址	private $url = Config::get('baidu.request.oauth_url');//百度鉴权地址	private $client_id 	= Config::get('baidu.request.client_id');//百度鉴权client_id	private $client_secret 	= Config::get('baidu.request.client_secret');//百度鉴权client_id对应secret    //重新获取鉴权认证    public function refresh(){		$post_data['grant_type']       = 'client_credentials';		$post_data['client_id']      = $this->client_id;		$post_data['client_secret'] = $this->client_secret;		$o = "";		foreach ( $post_data as $k => $v ) 		{			$o.= "$k=" . urlencode( $v ). "&" ;		}		$post_data = substr($o,0,-1);		        $data 	= $this->curl($this->url, $post_data);		Log::info(' get access_token result:'.json_encode($data));		return $data;    }    /* 	 * 发票识别	 * access_token   百度 access_token 鉴权返回值	 * files   发票图片文件	 */    public function vat_invoice($access_token, $files){		$file_name_array 	= explode('.', $files);		if(count($file_name_array) < 2){			return false;		}		$suffix 	= $file_name_array[count($file_name_array)-1];		//判断文件后缀		if(in_array(strtolower($suffix), ['jpg','jpeg','png','bmp'])){			$param_type 	= 'image';		}		else if(strtolower($suffix) == 'pdf'){			$param_type 	= 'pdf_file';		}		else{			return false;		}		$img = file_get_contents($files);		$img = base64_encode($img);		$param = array(			$param_type => $img		);		//这里也可以自己写curl方法调用		$data 	= $this->curl($this->vi_url.'?access_token='.$access_token, $param);		Log::info(' get result:'.json_encode($data));		return $data;    }	    //发票验真    //access_token 百度 access_token 鉴权返回值    public function vat_invoice_verification($access_token, $invoice_code, $invoice_num, $invoice_date, $invoice_type, $check_code, $total_amount){		$bodys = array(			'invoice_code' => $invoice_code,//"发票代码",			'invoice_num' => $invoice_num,//"发票号码",			'invoice_date' => $invoice_date,//"开票日期",			'check_code' => $check_code,//"校验码。填写发票校验码后6位",			'invoice_type' => $invoice_type,//"发票类型",			'total_amount' => $total_amount,//"不含税金额"		);				$data 	= $this->curl($this->viv_url.'?access_token='.$access_token, $bodys);		Log::info(' get result:'.json_encode($data));		return $data;    }    private function curl($url = '', $param = '')    {        if (empty($url) || empty($param)) {            return false;        }        $postUrl = $url;        $curlPost = $param;        $curl = curl_init();//初始化curl        curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);        $data = curl_exec($curl);//运行curl        //记录每次获取到的信息        Log::info(' get curl result:' . json_encode($data));        curl_close($curl);        return $data;    }}
 |