后台转base64

https://www.whmblog.cn/php/94.html

 

if($edit['user_identity_type'] == 1){
            header("Content-type:text/html;charset=utf-8");
            $key = 'd8aadc41573b0057507a8ff314dd241f';
            $base64_img = $this->Base64EncodeImage('.'.mb_substr($edit['user_identity_face_logo'], 44));//根据服务器传入图片地址
            $side = 'front';
            $arr = explode(',', $base64_img);
            $base64_img = $arr[1];
            //var_dump($base64_img);exit;
            $sendUrl = 'http://apis.juhe.cn/idimage/verify'; //短信接口的URL

            $smsConf = array(
                'key'   => $key, //您申请的APPKEY
                'image'    => $base64_img, //图片base_64
                'side'    => $side //身份证正反面
            );

            $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信

            if($content){
                $result = json_decode($content,true);

                $error_code = $result['error_code'];
                $res = $result['result'];

                if($error_code == 0 && $res['realname'] == $edit['user_realname'] && $res['idcard'] == $edit['user_identity_card']){
                    //实名认证成功
                    $edit['user_identity_statu'] = 2;

                }else{
                    $edit['user_identity_statu'] = 0;
                    return $this->data->addBody(-140, $res, __('认证失败'), 250);
                    //状态非0,说明失败
                }
            }else{
                //返回内容异常,以下可根据业务逻辑自行修改
                return $this->data->addBody(-140, array(), __('请求认证失败'), 250);
            }
        }

/**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 
Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    /**
     * 图片转base64
     * @param ImageFile String 图片路径
     * @return 转为base64的图片
     */
    function Base64EncodeImage($ImageFile) {
        if(file_exists($ImageFile) || is_file($ImageFile)){
            $base64_image = '';
            $image_info = getimagesize($ImageFile);
            $image_data = fread(fopen($ImageFile, 'r'), filesize($ImageFile));
            $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
            return $base64_image;
        }
        else{
            return false;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值