购买价钱及API文档链接:market.aliyun.com/products/57…
注意:仅能识别驾驶证信息,其他图片会报错
1、管理控制台找到购买的服务的appCode
2、参考代码:
<?php
class ocrVehicle
{
//套餐 AppCode
protected $appCode = 'd0fe4f1aec0449d49c865cd82c000324';
/**
* @param $image 识别的图片 行驶证正反面 本地路径
* @param string $side face=>正面 back=>反面
* @return hink
esponsearray code = 200 识别成功,code != 200 识别失败
* 正面返回值注释:
* addr=>'住址'
* config_str=>'正面/反面'
* engine_num=>'发动机号'
* issue_date=>'发证日期'
* model=>'品牌型号'
* owner=>'所有人'
* plate_num=>'号牌号码'
* register_date=>'注册日期'
* request_id=>'本次请求id'
* success=>''
* use_character=>'使用性质'
* vehicle_type=>'车辆类型'
* vin=>'车辆识别代号'
*
* 反面返回值注释:
* appproved_passenger_capacity=>'实际载人量'
* approved_load=>'核定载质量'
* approved_passenger_capacity=>'核定载人数'
* config_str=>'正面/反面'
* energy_type=>'能量类型'
* file_no=>'档案编号'
* gross_mass=>'总质量'
* inspection_record=>'检验记录'
* overall_dimension=>'外廓尺寸'
* plate_num=>'号牌号码'
* request_id=>'本次请求id'
* success=>''
* traction_mass=>'准牵引总质量'
* unladen_mass=>'整备质量'
*/
public function distinguish($image,$side='face')
{
$url = "https://dm-53.data.aliyun.com/rest/160601/ocr/ocr_vehicle.json";
//如果输入带有inputs, 设置为True,否则设为False
$is_old_format = false;
//如果没有configure字段,config设为空
$config = array(
"side" => $side
);
if($fp = fopen($image, "rb", 0)) {
$binary = fread($fp, filesize($image)); // 文件读取
fclose($fp);
$base64 = base64_encode($binary); // 转码
}
$headers = array();
array_push($headers, "Authorization:APPCODE " . $this->appCode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
if($is_old_format == TRUE){
$request = array();
$request["image"] = array(
"dataType" => 50,
"dataValue" => "$base64"
);
if(count($config) > 0){
$request["configure"] = array(
"dataType" => 50,
"dataValue" => json_encode($config)
);
}
$body = json_encode(array("inputs" => array($request)));
}else{
$request = array(
"image" => "$base64"
);
if(count($config) > 0){
$request["configure"] = json_encode($config);
}
$body = json_encode($request);
}
$method = "POST";
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$url, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
// $rheader = substr($result, 0, $header_size);
$rbody = substr($result, $header_size);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if($httpCode == 200){
if($is_old_format){
$output = json_decode($rbody, true);
$result_str = $output["outputs"][0]["outputValue"]["dataValue"];
}else{
$result_str = $rbody;
}
return sendArray(json_decode($result_str,true),$httpCode);
}else{
return sendArray([],$httpCode,$rbody);
}
}
}