Files
liche/common/libraries/Tcmarket.php
T
2022-09-15 14:40:21 +08:00

93 lines
3.2 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Desc: 腾讯云图片识别
* Date: 2021-07-20
* Time: 16:49
*/
class Tcmarket{
private $secretId;
private $secretKey;
private $source;
private $ci;
private $dir = 'tcmarket';
private $log_file = 'info.log';
public function __construct(){
$this->secretId = 'AKIDggwqp02pqewr9k3walN70j6c89i80957qnh1';
$this->secretKey = '5Ao8l7pTbUh4h4fun55ndkFoxhqo2k49Z1R6gz23';
$this->source = 'market';
$this->ci = & get_instance();
}
/**
* 保单识别 (文档地址:https://market.cloud.tencent.com/products/27659)
* @param $imgUrl
* @param $imgBase64
* @return array|void
*/
public function autoInsurance($imgUrl,$imgBase64=''){
if(!$imgUrl && !$imgBase64){
return ['code'=>0,'msg'=>'参数错误'];
}
// 签名
$datetime = gmdate('D, d M Y H:i:s T');
$signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $this->source);
$sign = base64_encode(hash_hmac('sha1', $signStr, $this->secretKey, true));
$auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $this->secretId, $sign);
// body参数(POST方法下)
$bodyParams = array (
'imgBase64' => $imgBase64,
'imgUrl' => $imgUrl,
);
// url参数拼接
$url = 'https://service-krag1mbm-1303991992.gz.apigw.tencentcs.com/release/autoInsurance';
$result = $this->http_curl($datetime,$auth,$url,$bodyParams);
debug_log("图片识别结果:".json_encode($result,JSON_UNESCAPED_UNICODE),$this->log_file,$this->dir);
if($result['status']==200){
return ['code'=>1,'data'=>$result['data'],'msg'=>'识别成功'];
}else{
return ['code'=>0,'msg'=>$result['message']];
}
}
private function http_curl($datetime,$auth,$url,$bodyParams){
// 请求方法
$method = 'POST';
// 请求头
$headers = array(
'X-Source' => $this->source,
'X-Date' => $datetime,
'Authorization' => $auth,
);
// url参数拼接
// $url = 'https://service-krag1mbm-1303991992.gz.apigw.tencentcs.com/release/autoInsurance';
// if (count($queryParams) > 0) {
// $url .= '?' . http_build_query($queryParams);
// }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
return $k . ': ' . $v;
}, array_values($headers), array_keys($headers)));
if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
}
$data = curl_exec($ch);
if (curl_errno($ch)) {
$data = ['status'=>400,'message'=>curl_errno($ch)];
}else{
$data = json_decode($data,true);
}
curl_close($ch);
return $data;
}
}