thinphp获取带参数的小程序码

   /**
     * 获取用户的分销码
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_fenxiao_qr(){
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token();
        $array = [
            //传参
            'scene'=>"pid=".$this->user_id,
            'page'=>'pages/index/index',
            'check_path'=>false
        ];
        //请求微信的获取小程序码接口
        $result = $this->posturl($url,$array);
        $res = json_decode($result);
        if(!empty($res)){
            return json_encode(message('调用腾讯接口失败',false));
        }
        //把二维码存到项目里
        $path = 'upload/qrcode/'.rand(100000,999999).uniqid().'.jpg';
        file_put_contents($path, $result);
        $http_type = $this->request->domain();
        $imgUrl = $http_type.'/'.$path;
        return json_encode(message('获取成功',true,$imgUrl));
    }
    /**
     * post调用第三方接口
     * @param $url
     * @param $data
     * @return mixed
     */
    public function posturl($url, $data)
    {
        $data = json_encode($data);
        $headerArray = ["Content-type:application/json;charset='utf-8'", 'Accept:application/json'];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * 获取小程序access_token
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_access_token(){
        $access_token = Cache::get('access_token');
        if (empty($access_token)){
            $settings=db('setting')->where('id', '1')->find();
            $appid=$settings['APPID'];
            $secret=$settings['APPSECRET'];
            $urlss = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $urlss);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $access_token = json_decode($output)->access_token;
            if(empty($access_token)){
                return json_encode(message('获取access_token失败',false));
            }
            Cache::set('access_token',$access_token,7000);
        }
        return $access_token;
    }

接口调用的是:获取不限制的小程序码并携带参数

官方文档地址:获取不限制的小程序码 | 微信开放文档

遇到的坑:最早的时候直接给前端返图片的二进制文件,结果参数丢失。后来把图片保存的服务器才正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值