PHP微信小程序分享获取二维码(带参数)

1.首先获取接口调用凭证 access_token

接口地址 :GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
	/**
     * 微信小程序获取接口调用凭证AccessToken
     */
       public function get_access_token()
    {
     
        $appid = config('wechat.app_id');//	小程序appid
        $secret = config('wechat.secret');   //  小程序appsecret

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";

        $user_obj = $this->curlHttp($url);
        session('access_token',$user_obj['access_token']);

        return $user_obj['access_token'];

    }


	/**
	 * 发送get请求
	 */
	public function curlHttp($url){
     
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL,$url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_HEADER, false);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	    $output = curl_exec($ch);
	    //释放curl句柄
	    curl_close($ch);
	    return json_decode($output,true);
	}
	

2.调用获取小程序码api

接口地址:POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

这里有几种接口地址,可以对照微信官方文档进行选择,官网地址:
链接: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html.

	    /**
     * 获取小程序码
     */
    public function get_unlimited()
    {
     
        $scene = request()->param('scene'); //参数
        $page = request()->param('page');   //小程序页面(必须真实,已发布的小程序页面地址)
        $access_token = session('access_token');
        if ($access_token) {
     
            $accessToken= $access_token;
        }else{
     
            $accessToken = $this->get_access_token();
        }
        $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";
        $data=[
            'scene'=>$scene,
            'page'=>$page,
            'width'=>600,
            'auto_color'=>false,
        ];
        $data=json_encode($data);

        //拿到二维码
        $result = $this->curl_post_https($url,$data);
        
        $result=$this->data_uri($result,'image/png');

       return $result;
    }

3.返回二进制转图片image/png(根据自己需求,这里是直接返回)

//二进制转图片image/png
    public function data_uri($contents, $mime)
    {
     
        $base64   = base64_encode($contents);
        return ('data:' . $mime . ';base64,' . $base64);
    }

你可能感兴趣的:(后台,PHP,小程序码,php)