<?php
// 数据层
namespace CommonModel;
use ThinkModel;
use VendorFuncRed;
class IdCardCheckModel extends Model{
public function checkIdCard($img_path,$id_card_side = 'front'){
$token = $this->getToken();
if (!$token){
$return_data = [
'num' => '-1',
'desc' => 'token获取失败'
];
return $return_data;
}
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
//$img_path = 'https://cdn.caomall.net/15555712831806909448.jpeg'; // 正面
//$img_path = 'https://cdn.caomall.net/1555572181863006213.jpeg'; // 反面
//$img_path = 'https://wx.qlogo.cn/mmopen/vi_32/61Y6viaroCFUu7J68k1GB9ibXiaAGQaCDn3rLiaDibZrqau7Dx94gDva2UkRZwicibIpkBdrpddjmpZu78ERgSPxMFp1g/132';
$img = file_get_contents($img_path);
$img = base64_encode($img);
$bodys = array(
"image" => $img,
"id_card_side" => $id_card_side,
"detect_direction" => true,
"detect_risk" => true
);
$res = $this->request_post($url, $bodys);
$res = json_decode($res,true);
if (!$res){
$return_data = [
'num' => '-200',
'desc' => '接口无返回'
];
return $return_data;
}
if ($res['image_status'] != 'normal'){
$return_data = [
'num' => '-2',
'desc' => '请上传正确图片'
];
return $return_data;
}
return $res;
}
/***
*
* {
"log_id":1952791012656395794,
"words_result_num":6,
"direction":0,
"image_status":"normal",
"words_result":{
"住址":{
"location":{
"width":159,
"top":195,
"height":38,
"left":138
},
"words":"云南省保山市隆阳区芒宽乡西亚村委会金勐1组"
},
"出生":{
"location":{
"width":127,
"top":164,
"height":16,
"left":137
},
"words":"19911029"
},
"姓名":{
"location":{
"width":52,
"top":102,
"height":17,
"left":137
},
"words":"孟小梅"
},
"公民身份号码":{
"location":{
"width":204,
"top":267,
"height":22,
"left":204
},
"words":"533001199110292467"
},
"性别":{
"location":{
"width":12,
"top":136,
"height":15,
"left":136
},
"words":"女"
},
"民族":{
"location":{
"width":12,
"top":135,
"height":12,
"left":215
},
"words":"汉"
}
}
}
*/
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
private 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;
}
private function request_post_access_token($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
public function getToken(){
$redis = Red::create();
$token_key = 'nsh_baidu_access_token';
$redis_access_token = $redis->get($token_key);
if ($redis_access_token){
return $redis_access_token;
}
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = '*******'; //你的 Api Key
$post_data['client_secret'] = '*******************'; //你的 Secret Key
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$res = $this->request_post_access_token($url, $post_data);
$res = json_decode($res,true);
$redis->set($token_key,$res['access_token'],(int)($res['expires_in'] / 2));
return $res['access_token'];
}
/***
* <pre>array(6) {
["refresh_token"] => string(72) "25.54c86eece3d142c1f95a79bd38e61e12.315360000.1870935687.282335-16049194"
["expires_in"] => int(2592000) //秒为单位,一般为1个月
["session_key"] => string(96) "9mzdX7m2z9HJ1/DdRnRqArZfQXWmjlyPJAtL/yLowHm7bLXd+CGBdQ90JmGX6YCcCzuIsqGB5aCsGOHEnL/xFevEd/tN1Q=="
["access_token"] => string(70) "24.dcee9fa9addf5dd12f33bb2812fb73f8.2592000.1558167687.282335-16049194"
["scope"] => string(971) "public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic brain_ocr_general_enhanced vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 brain_ocr_vin brain_ocr_quota_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi"
["session_secret"] => string(32) "d58142076b808065574c596be7dfcddc"
}
</pre>
*/
}
基于tp3.2 , model 封装开发
/*** * * { "log_id":1952791012656395794, "words_result_num":6, "direction":0, "image_status":"normal", "words_result":{ "住址":{ "location":{ "width":159, "top":195, "height":38, "left":138 }, "words":"云南省保山市隆阳区芒宽乡西亚村委会金勐1组" }, "出生":{ "location":{ "width":127, "top":164, "height":16, "left":137 }, "words":"19911029" }, "姓名":{ "location":{ "width":52, "top":102, "height":17, "left":137 }, "words":"孟小梅" }, "公民身份号码":{ "location":{ "width":204, "top":267, "height":22, "left":204 }, "words":"533001199110292467" }, "性别":{ "location":{ "width":12, "top":136, "height":15, "left":136 }, "words":"女" }, "民族":{ "location":{ "width":12, "top":135, "height":12, "left":215 }, "words":"汉" } } } */
参考百度文档:http://ai.baidu.com/docs#/Auth/top