php结合阿里云(印刷文字识别-身份证识别)进行身份证真实性验证------识别图片中的信息进行验证
程序员文章站
2024-03-16 10:10:52
...
1.购买阿里云的套餐:
2.查看一下你的AppCode:
3.代码部分:
/**
* 图片转码
*/
public function img_base64($path){
//对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
$img_data="";
if (substr($path,0,strlen("http")) === "http"){
$img_data = $path;
}else{
if($fp = fopen($path, "rb", 0)) {
$binary = fread($fp, filesize($path)); // 文件读取
fclose($fp);
$img_data = base64_encode($binary); // 转码
}else{
return 2;
// printf("%s 图片不存在",$img_path);
}
}
return $img_data;
}
/**
* 身份证真实性认证
* $img_path 图片路径
* $type 正面/反面
* $user_id 用户id
*/
public function getcard($img_path,$type=1,$user_id){
$url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
$appcode = "23e378360b4945be8338ef6ba9891a41";
// $img_path =$_SERVER['DOCUMENT_ROOT']. "/Uploads/2.jpg";
$method = "POST";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
//如果没有configure字段,config设为空
// $type=1;//1是正面
$side=$type=1?"face":"back";
$config = array(
"side"=>$side
);
$img_data=$this->img_base64($img_path);
$request = array(
"image" => "$img_data"
);
if(count($config) > 0){
$request["configure"] = json_encode($config);
}
$body = json_encode($request);
$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){
$result_str =json_decode( $rbody,true);
//***********************************************************//
if($type==1){
$userCard=array(
'name' =>$result_str['name'],
'address' =>$result_str['address'],
'birth' =>$result_str['birth'],
'nationality' =>$result_str['nationality'],
'num' =>$result_str['num'],
'sex' =>$result_str['sex'],
'user_id' =>$user_id,
'create_time' =>date('Y-m-d H:i:s'),
);
$iscard=M("user_idcard")->where(array('user_id'=>$user_id))->find();
if(empty($iscard)){
M("user_idcard")->add($userCard);
}
$ret['mess']='正面认证成功';
}else{
$ret['mess']='反面认证成功';
}
$ret['code']=1;
$this->ajaxReturn($ret);
//***************************这中间部分写你的业务逻辑就可以*********************//
// printf("result is :\n %s\n", $result_str);
}else{
$ret['mess']='正面认证失败';
$ret['code']=2;
$this->ajaxReturn($ret);
// printf("Http error code: %d\n", $httpCode);
//printf("Error msg in body: %s\n", $rbody);
//printf("header: %s\n", $rheader);
}
}
上一篇: 微服务项目框架搭建
下一篇: 创作能手——LSTM“诗人”