php生成带参数的小程序码

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

	public function getCode()
    {
    	$appid = '';
    	$appsecret = '';
        $access_token = $this->accessToken($appid, $appsecret);
        $width = 430;//二维码宽度
        $page = '';//小程序路径(pages/index/index)
        $scene = '';//携带的参数(?id=1&tag=2)

        $wx_api = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
        $data = [
            'page'      => $page,
            'scene'     => $scene,
            'width'     => $width,
        ];

        $post_data  = json_encode($data);

        $result     = $this->api_notice_increment($wx_api, $post_data);     //获取微信小程序二维码
        if(!$result){
            return false;
        }
		$img_base64 = base64_encode($result);
        echo '<img src="' . $img_base64 . '">';
    }

	public function accessToken($APP_ID, $SECRET)
    {
        $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $APP_ID . "&secret=" . $SECRET;
        $res = file_get_contents($token_access_url);
        $result = json_decode($res, true);
        $access_token = $result['access_token'];
        return $access_token;
    }

	function api_notice_increment($url, $data)
	{
        $ch = curl_init();
        $header = array("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($ch, 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{
            // var_dump($tmpInfo);
            return $tmpInfo;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值