{ "log_id": 490058765, "words_result": { "单位名称": { "location": { "left": 500, "top": 479, "width": 618, "height": 54 }, "words": "袁氏财团有限公司" }, "类型": { "location": { "left": 53, "top": 64, "width": 74, "height": 97 }, "words": "有限责任公司(自然人独资)" }, "法人": { "location": { "left": 938, "top": 557, "width": 94, "height": 46 }, "words": "袁运筹" }, "地址": { "location": { "left": 503, "top": 644, "width": 574, "height": 57 }, "words": "江苏省南京市中山东路19号" }, "有效期": { "location": { "left": 779, "top": 1108, "width": 271, "height": 49 }, "words": "2015年02月12日" }, "证件编号": { "location": { "left": 1219, "top": 357, "width": 466, "height": 39 }, "words": "苏餐证字(2019)第666602666661号" }, "社会信用代码": { "location": { "left": 0, "top": 0, "width": 0, "height": 0 }, "words": "无" } }, "words_result_num": 6 }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/3/13 17:10 */ //---------------------------------- // 百度营业执照识别调用类 //---------------------------------- class freeApi{ private $apiKey = false; //百度应用AppID private $secretKey = false; //百度应用API Key private $tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token'; private $apiUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license'; public function __construct($apikey,$secretkey){ $this->apiKey = $apikey; $this->secretKey = $secretkey; } /** * 获取token * @return array */ public function getToken(){ $params = [ 'grant_type' => 'client_credentials', 'client_id' => $this->apiKey, 'client_secret' => $this->secretKey, ]; $params = $this->handleUrl($params); return $this->returnArray($this->freeApiCurl($this->tokenUrl,$params,1)); } /** * url拼接 * @return string */ private function handleUrl($params){ $o = ""; foreach ( $params as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $params = substr($o,0,-1); return $params; } /** * 将JSON内容转为数据,并返回 * @param string $content [内容] * @return array */ public function returnArray($content){ return json_decode($content,true); } /** * 获取营业执照证识别结果 * @return array */ public function getResult(){ $params = [ 'image' => base64_encode(file_get_contents('图片路径')), ]; return $this->returnArray($this->freeApiCurl($this->apiUrl.'?access_token='.$this->getToken()['access_token'],$params,1)); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'chuanshuoapi' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; } }