一般情况调用phpqrcode第三方插件 会把生成的二维码图片保存到服务器,不保存服务器也会以header头的形式输出到浏览器,(我们不允许把图片文件保存的liunx服务器,只能保存到阿里云OSS存储服务器  不符合我的逻辑)所有经过我的日思夜想,于是想出来的实现方式 。我可不可以跟爬虫一样先把这个图片文件资源先获取到,然后直接在后端上传oss

createQrcode 生成二维码方法

    public function createQrcode(){
        if(!$_POST['str'])  return false;
        $str = $_POST['str'];
        require_once ROOT.'cube/action/phpqrcode/phpqrcode/qrlib.php';         //包含类文件

        $QRcode = new \QRcode();
        //允许的错误级别
        $errorCorrectionLevel = 'L';
        if (isset($level) && in_array($level, array('L','M','Q','H')))
            $errorCorrectionLevel = $level;   

        //二维码大小
        $matrixPointSize = 4;
        if (isset($_POST['size'])){
            $size = $_POST['size'];
            $matrixPointSize = min(max((int)$size, 1), 10);
        }
        if (isset($str)) {

            $QRcode->png($str, false, $errorCorrectionLevel, $matrixPointSize);

        }else{
            return "ERRORDATA";
        }     

    }

uploadqrcode 获取二维码上传OSS

    public function uploadqrcode(){
        //生成而我加密字符串
        $str = 'orderid=111';    // 被加密信息
        $key = 'resss';    // 密钥
        $strs = A('strlib/basic/encrypt',array($str, $key));//加密后的字符串

        $name = date('Y-m-d').A('strlib/basic/randstr',array(6,3));
        /*
        //模拟get请求
        $data = array(
            'name'=>'zhezhao',
            'age'=>'23'
            );
        $query = http_build_query($data);

        $url = 'http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行

        $result = file_get_contents($url.'?'.$query);
        */
        //这里为了安全性我选择post请求
        $data = array(
            'str'=>$strs
            );

        $query = http_build_query($data);

        $options['http'] = array(
             'timeout'=>60,
             'method' => 'POST',
             'header' => 'Content-type:application/x-www-form-urlencoded',
             'content' => $query
            );

        $url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/order/lineOrder/createQrcode';
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        A('oss/object/putObject',array('bucket',$_SERVER['HTTP_HOST'].'/lineorder/qrcode/'.$name.'.png',$result));
    }

这里遇到一个问题就是我需要把二维码的参数传递个createQrcode  所有想到用模拟表单

另附字符串加密方法 (可逆向解密)

//加密    
public function encrypt($data, $key)
    {
        $key    =    md5($key);
        $x        =    0;
        $len    =    strlen($data);
        $l        =    strlen($key);
        $char = $str = '';
        for ($i = 0; $i < $len; $i++)
        {
            if ($x == $l)
            {
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++)
        {
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }
//解密
 public function decrypt($data, $key)
    {
        $key = md5($key);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        $char = $str = '';
        for ($i = 0; $i < $len; $i++)
        {
            if ($x == $l)
            {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++)
        {
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
            {
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            }
            else
            {
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }
        return $str;
    }

生成随机字符串

    // 数值型字符串,代号 1
    private $cs1 = array('0','1','2','3','4','5','6','7','8','9');
    // 纯小写字符串,代号 2
    private $cs2 = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    // 多类型字符串,代号 3
    private $cs3 = array
    (
        '0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
    );

    public function randstr($len,$type=1)
    {
        if( $len<1 ) return false;
        $RET = '';
        switch( $type )
        {
            case 1:
                $RET = $RET.$this->randcs1(false);
                for( $i=2; $i<=$len; $i++ ) $RET = $RET.$this->randcs1();
                return $RET;
            case 2:
                for( $i=1; $i<=$len; $i++ ) $RET = $RET.$this->cs2[rand(0,25)];
                return $RET;
            case 3:
                $RET = $RET.$this->randcs3(false);
                for( $i=2; $i<=$len; $i++ ) $RET = $RET.$this->randcs3();
                return $RET;
            default:
                return false;
        }
    }

    private function randcs1($zero=true)
    {
        $from = 1;
        if( $zero ) $from = 0;
        return $this->cs1[rand($from,9)];
    }

    private function randcs3($zero=true)
    {
        $from = 1;
        if( $zero ) $from = 0;
        return $this->cs3[rand($from,61)];
    }
posted on 2017-12-20 10:56  丿小橙子  阅读(56)  评论(0编辑  收藏  举报