雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

身份证图片识别、银行卡图片识别接口(阿里云)

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天前
赞一波!

文章评论

全部评论