<?php
|
function access_post($url = '', $param = '') {
|
if (empty($url) || empty($param)) {
|
return false;
|
}
|
|
$postUrl = $url;
|
$curlPost = $param;
|
$api_info=file_get_contents($postUrl."?".$curlPost);
|
$api_info=json_decode($api_info,true);
|
$access_token = $api_info["access_token"];
|
//if ($result!=1) {header("Location: /"); exit;}
|
return $access_token;
|
}
|
|
$url = 'https://aip.baidubce.com/oauth/2.0/token';
|
$post_data['grant_type'] = 'client_credentials';
|
$post_data['client_id'] = 'qLaZeiLR3pnb03qto3OPoFtG';
|
$post_data['client_secret'] = 'TmpfWyGGy3xtDdESUz9yjtQ4PyOk8YEP';
|
$o = "";
|
foreach ( $post_data as $k => $v )
|
{
|
$o.= "$k=" . urlencode( $v ). "&" ;
|
}
|
$post_data = substr($o,0,-1);
|
|
|
function request_post($url = '', $param = '')
|
{
|
if (empty($url) || empty($param)) {
|
return false;
|
}
|
|
$postUrl = $url;
|
$curlPost = $param;
|
// ³õʼ»¯curl
|
$curl = curl_init();
|
curl_setopt($curl, CURLOPT_URL, $postUrl);
|
curl_setopt($curl, CURLOPT_HEADER, 0);
|
// ÒªÇó½á¹ûΪ×Ö·û´®ÇÒÊä³öµ½ÆÁÄ»ÉÏ
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
// postÌá½»·½Ê½
|
curl_setopt($curl, CURLOPT_POST, 1);
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
|
// ÔËÐÐcurl
|
$data = curl_exec($curl);
|
curl_close($curl);
|
|
return $data;
|
}
|
$method = !empty($_REQUEST["method"])?$_REQUEST["method"]:"";
|
$a = empty($_REQUEST['a'])!=false ? 0 : $_REQUEST['a'];
|
$res="";
|
|
switch ($method)
|
{
|
case "meter": //ÒÇÆ÷ÒDZíÅ̶ÁÊýʶ±ð
|
$image = empty($_REQUEST['image'])!=false ? "" : $_REQUEST['image'];
|
|
if ($image!="") {
|
$access_token = access_post($url, $post_data);
|
$token = $access_token;
|
//ÒÇÆ÷ÒDZíÅ̶ÁÊýʶ±ð
|
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/meter?access_token=' . $token;
|
$img = file_get_contents($image);
|
$img = base64_encode($img);
|
$bodys = array(
|
'image' => $img
|
);
|
$api_json = request_post($url, $bodys);
|
//echo $api_json."<br>";//exit;
|
$api_info=json_decode($api_json,true);
|
|
|
$error_code = $method = !empty($api_info["error_code"]) ? $api_info["error_code"] : 0;
|
if ($error_code==0) {
|
$res=0;
|
foreach($api_info["words_result"] as $x=>$x_value) {
|
$words= !empty(intval($x_value["words"])) ? intval($x_value["words"]) : 0;
|
if ($words>$res) {
|
$res=$words;
|
}
|
}
|
}else{
|
$res = -1; //ʶ±ðʧ°Ü
|
}
|
//if ($res<=100) {
|
// //Êý×Öʶ±ð
|
// $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?access_token=' . $token;
|
// $api_json = request_post($url, $bodys);
|
// $api_info=json_decode($api_json,true);
|
// $error_code = $method = !empty($api_info["error_code"]) ? $api_info["error_code"] : 0;
|
// if ($error_code==0) {
|
// $res=0;
|
// foreach($api_info["words_result"] as $x=>$x_value) {
|
// $words= !empty(intval($x_value["words"])) ? intval($x_value["words"]) : 0;
|
// if ($words>$res) {
|
// $res=$words;
|
// }
|
// }
|
// }
|
//}
|
}else{
|
$res = -2; //ûͼƬ
|
}
|
break;
|
|
case "accurate": //ͨÓÃÎÄ×Öʶ±ð£¨¸ß¾«¶Èº¬Î»Öð棩
|
$image = empty($_REQUEST['image'])!=false ? "" : $_REQUEST['image'];
|
|
if ($image!="") {
|
$access_token = access_post($url, $post_data);
|
$token = $access_token;
|
//ÒÇÆ÷ÒDZíÅ̶ÁÊýʶ±ð
|
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=' . $token;
|
$img = file_get_contents($image);
|
$img = base64_encode($img);
|
$bodys = array(
|
'image' => $img
|
);
|
$api_json = request_post($url, $bodys);
|
echo $api_json."<br>";exit;
|
$api_info=json_decode($api_json,true);
|
|
|
$error_code = $method = !empty($api_info["error_code"]) ? $api_info["error_code"] : 0;
|
if ($error_code==0) {
|
$res=0;
|
foreach($api_info["words_result"] as $x=>$x_value) {
|
$words= !empty(intval($x_value["words"])) ? intval($x_value["words"]) : 0;
|
if ($words>$res) {
|
$res=$words;
|
}
|
}
|
}else{
|
$res = -1; //ʶ±ðʧ°Ü
|
}
|
//if ($res<=100) {
|
// //Êý×Öʶ±ð
|
// $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?access_token=' . $token;
|
// $api_json = request_post($url, $bodys);
|
// $api_info=json_decode($api_json,true);
|
// $error_code = $method = !empty($api_info["error_code"]) ? $api_info["error_code"] : 0;
|
// if ($error_code==0) {
|
// $res=0;
|
// foreach($api_info["words_result"] as $x=>$x_value) {
|
// $words= !empty(intval($x_value["words"])) ? intval($x_value["words"]) : 0;
|
// if ($words>$res) {
|
// $res=$words;
|
// }
|
// }
|
// }
|
//}
|
}else{
|
$res = -2; //ûͼƬ
|
}
|
break;
|
}
|
//var_dump($res);
|
if ($a!=0) {
|
echo $api_json;
|
}else{
|
echo $res;
|
}
|
?>
|