PHP实现小程序OCR实名认证demo

2 篇文章 0 订阅
2 篇文章 0 订阅

PHP实现小程序OCR实名认证

在这里插入图片描述
此demo仅用于身份证识别,OCR内所有基本都通用,修改一下请求的接口及类型即可

基于laravel框架

public function demo(Request $request){
	###文件上传至本地及获取绝对路径
	$file = $request->file('file');
    $newFile = date('Y-m-d');
    $url_path = 'uploads/'.$newFile;
    if ($file->isValid()) {
        $clientName = $file->getClientOriginalName();
        $entension = $file->getClientOriginalExtension();
        $newName = md5(date("Y-m-d H:i:s") . $clientName) . "." . $entension;
        $file->move($url_path, $newName);
        $namePath = $url_path . '/' . $newName; #文件绝对路径
    }
   ###进入正题
   $file =  $namePath;  #获取本地文件路径
   $type = 'photo';
   #配置
   $APPID = APPID;#小程序id
   $APPSECRET = APPSEC;#小程序密钥
   #获取access_token
  $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
 //缓存access_token
   session_start();
   $_SESSION['access_token'] = "";
   $_SESSION['expires_in'] = 0;
   if (!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in']))
   {
       $json = file_get_contents($access_token);
       $json = json_decode($json, true);
       $_SESSION['access_token'] = $json['access_token'];
       $_SESSION['expires_in'] = time() + 7200;
       $ACCESS_TOKEN = $json["access_token"];
   } else
   {
       $ACCESS_TOKEN = $_SESSION["access_token"];
   }
   #判断是否缓存access_token
   if (!empty($ACCESS_TOKEN))
   {
   		$url = "https://api.weixin.qq.com/cv/ocr/idcard?access_token=".$ACCESS_TOKEN."&type=".$type;
   		if(class_exists('\CURLFile')){
           $data = ['img' => new \CURLFile($file)];
        }else{
            $data = ['img' => "@{$file}"];
        }
       
        $ch  = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res  = curl_exec($ch);
        curl_close($ch);
        $wxrs_id_card=json_decode($res,true);
        
        return $wxrs_id_card; #输出结果
   }else{
        return "access_token获取失败,请重试";
   }
}   

返回示例:
{
“errcode”: 0,
“errmsg”: “ok”,
“type”: “Front”,#正面
“name”: “云墨”,#名称
“id”: “XXXXXXXXXXXXXXXXX”,#身份证号码
“addr”: “身份证详细地址”,
“gender”: “男”,
“nationality”: “汉”,
“birth”: “2002-12-12”,
“card_property”: 4
}

详见微信开发文档:
https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/img-ocr/ocr/idCardOCR.html

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值