PHP 获取微信小程序码、二维码、B接口

 PHP后端代码

curl_get($url);
        $json = json_decode($data, true);
        return $json['access_token'];
    }
    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;
    }
//获得二维码
//get_qrcode($token,$scene,$page,$path);
    function get_qrcode($id,$cateid)
    {
        // header('content-type:image/gif');
        //header('content-type:image/png');格式自选,不同格式貌似加载速度略有不同,想加载更快可选择jpg
        header('content-type:image/jpg');
        $access_token= $this->get_access_token();
        $token = $access_token;   //请参考上面方法
        $scene = $id.'-'.$cateid;//"179-16";    //要传的参数
        $page = 'pages/datail/datail';          //跳转的路径(不填默认首页) pages/datail/datail?id=179&cateid=16
        $path = './Public/qrcode/'.$scene.'.jpg';        //二维码保存的路径
        $uid = 6;
        $data = array();
        $data['scene'] = $scene;
        $data['page'] = $page;
        $data = json_encode($data);
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
        $da = $this->api_notice_increment($url,$data);
        file_put_contents($path, $da);
       
    }
    function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($url, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }
}

小程序前端获取参数

  //微信小程序js文件中查看scene所带的参数
    Page({
      onLoad: function(options) {
        // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
        var scene = decodeURIComponent(options.scene)
        console.log(scene)
      }
    })
    //我后端传的参数为 $scene = $id.'-'.$cateid;//"179-16";  
    //使用console.log(scene);得到的结果为 uid=179-16
    //根据自己传参的类型,取出参数
      var id = scene.split("-")[0]; 
      var cateid = scene.split("-")[1];

    //以上的就完成了,但顺便说一下,如果是console.log(scene);得到的结果为 uid=179;这种格式
    //获得uid 的值
    //var uid = scene.split("=")[1];/

学习https://my.oschina.net/hcfblog/blog/1799752

你可能感兴趣的:(微信小程序,php,PHP,获取微信小程序接口B二维码接口,php,小程序,小程序码)