身份证图片识别、银行卡图片识别接口(阿里云)
2024-09-12
11
1. 前言
身份证图片识别:https://market.aliyun.com/products/57124001/cmapi010401.html
银行卡图片识别:https://market.aliyun.com/products/57124001/cmapi016870.html
阿里云OCR印刷文字识别接口一览:https://www.aliyun.com/activity/bigdata/ocrprodpromotionjuly
2. 使用示例
use app\lib\AliYun;try { // 身份证图片正面识别 $result = app(AliYun::class)->idCardOcr($img, 1); // 身份证图片反面识别 $result = app(AliYun::class)->idCardOcr($img, 2); // 银行卡图片识别 $result = app(AliYun::class)->bankCardOcr($img);} catch (\Throwable $th) { echo $th->getMessage();}
3. 阿里云接口功能封装
<?phpnamespace app\lib;/** * 阿里云接口封装 */class AliYun{ /** * 初始化配置参数 */ public function __construct() { // 阿里云 云市场 AppCode $this->appcode = "85ac5eff462e433ea373b2744c7xxxx"; } // +---------------------------------------------- // | OCR 印刷文字识别 // +---------------------------------------------- /** * 身份证识别 * * @param string $img 图片绝对路径或URL地址 * @param integer $side 身份证正反面类型:face(1)/back(2) */ public function idCardOcr(string $img, int $side) { $url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"; $body = ['image' => $this->img_base64($img)]; //如果没有configure字段,config设为空 //身份证正反面类型:face/back $config = ['side' => $side == 1 ? 'face' : 'back']; count($config) > 0 && $body["configure"] = $config; return $this->curlPost($url, $body); } /** * 银行卡识别 * * @param string $img */ public function bankCardOcr(string $img) { $url = "https://yhk.market.alicloudapi.com"; $path = "/rest/160601/ocr/ocr_bank_card.json"; $url = $url . $path; $body = [ 'image' => $this->img_base64($img), 'configure' => [ 'card_type' => true ], ]; return $this->curlPost($url, $body); } /** * curl post 请求封装 * * @param string $url * @param array $body */ private function curlPost(string $url, array $body) { $method = "POST"; $headers = array(); array_push($headers, "Authorization:APPCODE " . $this->appcode); //根据API的要求,定义相对应的Content-Type array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 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); } $body = json_encode($body); curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $result = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($httpCode == 200) { $result = json_decode($result, true); if (isset($result['success']) && $result['success'] === true) { return $result; //识别成功 } else { throw new \Exception('识别失败', 201); } } else { throw new \Exception('图片错误', 201); } } /** * 返回图片的base64或URL地址 * * @param string $path 绝对路径或URL地址 */ private function img_base64(string $path) { //对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回 $img_data = ""; if (substr($path, 0, strlen("http")) === "http") { // 网络地址图片 $img_data = $path; } else { // 绝对地址图片 if ($fp = fopen($path, "rb", 0)) {<code class="hljs ruby更新于:7天前
赞一波!
相关文章
- 微信公众号模板消息接口
- PHP 手机号归属地查询接口【阿里云】
- 阻止HTML表单提交跳转页面,使用 Jquery Ajax 请求接口
- 接口文档 API 调试、管理工具
- 阿里云对象存储OSS及CDN加速配置
- PHP通过CURL的方式读取图片接口输出乱码
- Vue cli4 图片地址引入的几种方式
- 阿里云短信服务验证码封装 v1.0.1【flc/dysms】
- 前端请求PHP接口,报错跨域问题
- 阿里云短信服务验证码封装类 v1.0.0【flc/dysms】
- 阿里云短信服务验证码介绍
- 在TP中生成二维码图片出现乱码
- 人人商城 V3.18.1 小程序端获取商品列表接口
- 使用Python爬虫下载某网站图片
- 使用Java实现图片文字识别
- C#正则表达式获取所有图片src
- Java中的微信支付:从接口调用到订单处理
- .NET接口压力测试指标
- 微信传输图片至电脑时如何解决图片不清晰的问题
- Java调用IP代理接口实现软件IP转换
文章评论
全部评论