新版-GET方法
public function newok(){
$ImageUrl = 'http://ceshi.com/ceshi1.jpg'; //图片URL
$CardSide = 'FRONT'; //FRONT 为行驶证主页正面(有红色印章的一面)BACK 为行驶证副页正面(有号码号牌的一面)。
$Action = 'VehicleLicenseOCR';
$SecretId = 'AKIDGGbiaDChhYGyYttvkZ7ucYE7k6QTHi'; //API密钥中的SecretId
$secretKey = 'IJ4dy1o9Dm57tRj1PWOzJNANNJ2W'; API密钥中的SecretKey
$Timestamp = time();
$Nonce = rand(1000,9999);
$Region = 'ap-beijing';
$Version = '2018-11-19';
$param["Nonce"] =$Nonce;
$param["Timestamp"] = $Timestamp;
$param["Region"] = $Region;
$param["SecretId"] = $SecretId;
$param["Action"] = $Action;
$param["Version"] = $Version;
$param["ImageUrl"] = $ImageUrl;
$param["CardSide"] = $CardSide;
$param["Language"] = "zh-CN";
//排序
ksort($param);
//拼接
$signStr = "GETocr.ap-beijing.tencentcloudapi.com/?";
foreach ( $param as $key => $value ) {
$signStr = $signStr . $key . "=" . $value . "&";
}
$signStr = substr($signStr, 0, -1);
//使用HMAC-SHA1算法,将上一步中获得的签名原文字符串进行加密,并对加密后的签名串使用Base64编码,得到最终的签名串
//对加密后的签名串使用urlencode编码,得到最终的签名串
$signature = urlencode(base64_encode(hash_hmac("sha1", $signStr, $secretKey, true)));
$URL = "https://ocr.ap-beijing.tencentcloudapi.com/?Action=".$Action."&Version=".$Version."&Region=".$Region."&ImageUrl=".$ImageUrl."&CardSide=".$CardSide."&Timestamp=".$Timestamp."&Nonce=".$Nonce."&SecretId=".$SecretId."&Signature=".$signature."&Language=zh-CN";
print_r($this->get_url($URL)); //调用get 方法的curl
}
/*curl get方法*/
public function get_url($durl){
// header传送格式
$headers = array(
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $durl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
输出示例
{
"Response": {
"FrontInfo": {
"PlateNo": "沪AA1234",
"VehicleType": "小型轿车",
"Owner": "李明",
"Address": "上海市徐汇区田林路397号腾云大厦6F",
"UseCharacter": "非营运",
"Model": "别克牌SGM7151LAAA",
"Vin": "ABCDEFGH123456789",
"EngineNo": "8B54321",
"RegisterDate": "2011-10-10",
"IssueDate": "",
"Seal": "上海市公安局交通警寨总队"
},
"BackInfo": null,
"RecognizeWarnCode": [
-9106
],
"RecognizeWarnMsg": [
"WARN_DRIVER_LICENSE_PS_CARD"
],
"RequestId": "820916b4-b391-40a8-9203-7ae87e3f1954"
}
}
旧版-POST方法
public function oldok(){
$url = 'https://recognition.image.myqcloud.com/ocr/drivinglicence';
//$data中的数据换成自己的
$data = [
'appid' => '1252358879', //接入项目的唯一标识,可在账号信息或云API密钥中查看
'type' => '0', //识别类型,0 表示行驶证,1 表示驾驶证,2 表示行驶证副页。
'url' => 'http://ceshi.com/ceshi1.jpg', //图片 url。
];
$data = json_encode($data); //将数据转为json格式
$aa = $this->post_url($url,$data); //此处调用curl 在该方法中配置 请求头!!!
print_r($aa);
}
public function post_url($durl, $post_data){
//请求头配置
$headers = array(
"host:recognition.image.myqcloud.com",
"content-type:application/json",
"authorization:".$this->authorization(), //鉴权算法 下方查看
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $durl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/*鉴权算法*/
public function authorization(){
$appid = "1252358879"; //接入项目的唯一标识,可在账号信息或云API密钥中查看
$bucket = "";
$secret_id = "AKIDGGbiaDChhYGyYttvkZ7uE7k6QTHvti"; //API密钥中的SecretId
$secret_key = "IJ4dy1o9Dm57tRj1PWOzJNEoANNJ2W"; //API密钥中的SecretKey
$expired = time() + 2592000;
$current = time();
$rdm = rand();
$str = 'a='.$appid.'&b='.$bucket.'&k='.$secret_id.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&f=';
$signStrOnce = base64_encode(hash_hmac('SHA1',$str,$secret_key, true).$str);
return $signStrOnce;
}
输出示例
{
"code":0,
"message":"OK",
"data":{
"session_id":"10000018324840",
"items":[
{
"item":"证号",
"itemcoord":{
"x":226,
"y":66,
"width":194,
"height":30
},
"itemconf":0.9999638199806212,
"itemstring":"342221199005033256"
},
{
"item":"姓名",
"itemcoord":{
"x":84,
"y":98,
"width":93,
"height":26
},
"itemconf":0.999157190322876,
"itemstring":"张三"
},
{
"item":"性别",
"itemcoord":{
"x":291,
"y":100,
"width":23,
"height":27
},
"itemconf":0.9999893307685852,
"itemstring":"男"
},
{
"item":"国籍",
"itemcoord":{
"x":388,
"y":100,
"width":44,
"height":28
},
"itemconf":0.9999999403953552,
"itemstring":"中国"
},
{
"item":"住址",
"itemcoord":{
"x":87,
"y":126,
"width":286,
"height":30
},
"itemconf":0.5020201206207275,
"itemstring":"深圳市南山区万利达大厦"
},
{
"item":"出生日期",
"itemcoord":{
"x":224,
"y":195,
"width":106,
"height":25
},
"itemconf":0.999338924884796,
"itemstring":"1990-05-03"
},
{
"item":"领证日期",
"itemcoord":{
"x":249,
"y":226,
"width":112,
"height":26
},
"itemconf":0.9999741315841676,
"itemstring":"2014-12-25"
},
{
"item":"准驾车型",
"itemcoord":{
"x":262,
"y":261,
"width":58,
"height":27
},
"itemconf":0.9999993443489076,
"itemstring":"C1"
},
{
"item":"起始日期",
"itemcoord":{
"x":122,
"y":291,
"width":111,
"height":26
},
"itemconf":0.9859412312507628,
"itemstring":"2014-12-25"
},
{
"item":"有效日期",
"itemcoord":{
"x":266,
"y":294,
"width":113,
"height":27
},
"itemconf":0.9921129941940308,
"itemstring":"2020-12-25"
},
{
"item":"红章",
"itemcoord":{
"x":52,
"y":186,
"width":110,
"height":100
},
"itemconf":0.9392985105514526,
"itemstring":"深圳市公安局交通管理局"
}
]
}
}
纯属自行研究,欢迎大佬指导