关于阿里云图片识别接口的demo

时间:2024-01-11 17:20:32

服务器处理过程

$host = "https://dm-53.data.aliyun.com";
$path = "/rest/160601/ocr/ocr_vehicle.json";
$method = "POST";
$appcode = "你自己申请的appcode";

$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
// array_push($headers,"Expect".":"."100-continue");
array_push($headers,"Expect:");
$querys = "";
$bodys = "{
\"inputs\":[
{
\"image\":{
\"dataType\":50,
\"dataValue\":\"".$base64."\"
}
}
]
}";
$url = $host . $path;
// var_dump($headers);exit;
$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);
// curl_setopt($curl, CURLOPT_HTTPHEADER, array( '"Expect":"100-continue"'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Expect:' ) );

if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
if(!curl_errno($curl)){
$info = curl_getinfo($curl);
echo "<pre>";
// var_dump($info);
echo "</pre>";
// echo "<pre>";
// $rtn=json_decode($rtn,true);
// echo $rtn;
// echo "</pre>";
} else {
echo 'Curl error: ' . curl_error($curl);
}

返回数据的处理

$response=curl_exec($curl);
$res=substr($str,strpos($str,"dataValue")+13);
// $own=substr($str,strpos($str,"owner"));
$a=substr($res,0,strrpos($res,"}")-4);
$b=stripslashes($a);
$pl=strpos($b,"plate_num");

$ow=strpos($b,"owner");
$len=$pl-$ow;
$own=mb_substr($b,strpos($b,"owner")+2,$len-17);
$c=json_decode($b,TRUE);