美文网首页
php生成带参数微信小程序二维码

php生成带参数微信小程序二维码

作者: charmingcheng | 来源:发表于2018-10-09 13:46 被阅读0次
    <?php
    class wxcode {
        private $appid;
        private $secret;
        private $id;
        private $width;
    
        public function __construct($appid = '', $secret = '', $id = 0, $width = '430')
        {
            $this->appid = $appid;
            $this->secret = $secret;
            $this->id = $id;
            $this->width = $width;
        }
    
        //获取access_token
        public function get_access_token()
        {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
            return $data = $this->curl_get($url);
        }
    
        public function curl_get($url) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            return $data;
        }
        
        //获得二维码
        public function get_qrcode() {
            //header('content-type:image/gif');
            //header('content-type:image/png');格式自选,不同格式貌似加载速度略有不同,想加载更快可选择jpg
            header('content-type:image/jpg');
            $id = $this->id;
            $data = array();
            $data['scene'] = "id=".$id;
            $data['page'] = "pages/product/show";  //参数跳转到product/show,产品详情
            $data['width'] = $his->width;
            $data = json_encode($data);
            $access = json_decode($this->get_access_token(),true);
            $access_token= $access['access_token'];
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
            $da = $this->get_http_array($url,$data);
        }
        public function get_http_array($url,$post_data) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出,不用print_r();也会在后面多个1
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
            $out = json_decode($output);
            return $out;
        }
    }

    相关文章

      网友评论

          本文标题:php生成带参数微信小程序二维码

          本文链接:https://www.haomeiwen.com/subject/euawiftx.html