用百度接口验证是否上传了身份证图片信息[非姓名,身份证号匹配]

    public function fuckIt(){

        $token = $this->getToken();
        if (!$token){
            return 'token获取失败';
        }

        $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"      => 'front',
            "detect_direction"  =>  true,
            "detect_risk"       =>  true
        );

        $res = $this->request_post($url, $bodys);
        var_dump($res);
    }




    /**
     * 发起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(){

        $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);

        return $res['access_token'];
    }
    /***
        *
     * {
    "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

猜你喜欢

转载自www.cnblogs.com/pansidong/p/10730068.html
今日推荐