PHP实现生成小程序二维码带参数进入指定页面

1.需求场景

1.小程序一定要是发布的小程序。2.记得把业务域名加上。

场景:需要在网页上生成小程序的二维码图片,微信扫码之后带参数进入指定的小程序页面

小程序在扫描二维码跳转的指定页面中,在onload里面接收参数。

注意scene是后台返回的,不是特定的参数名,不清楚的可以在扫描二维码之后进入的页面打印option查看。

获取到参数值之后需要用到decodeURIComponent进行解密,解密后的格式一般是 a=b&c=d的格式。

可以尝试以下方法截取:

onLoad(option){
    let queryAll = decodeURIComponent(option.scene)
    var a = queryAll.split('&')
    var obj = new Object
    for(let i in a){
        var b = a[i].split('=')
     obj[b[0]] = b[1]
}
console.log(obj);

 2.代码及说明

扫二维码对应模拟小程序入口场景中扫码进入小程序。可以路径传参和scene传参。然后再指定要跳转的页面路径。下面是单php文件,直接运行做演示用的。这里我走的路径传参。生成的图片在同路径uploads目录下,记得提前创建好这个目录哦~

<?php
	header("Content-Type: text/html; charset=utf-8");
	function test($code){  //code为参数
	    	$appid = 'xxx';
	    	$secret = 'xxxx';
	    	$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
	        //开启session
	        session_start(); 
	        // 保存2小时 
	        $lifeTime = 2 * 3600; 
	        setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
	        // echo $url;
	        $access_token = $_SESSION['access_token'];
	        if(empty($access_token)){
	            $access_token_data = getJson($url);
	            $access_token = $access_token_data['access_token'];
	            $_SESSION['access_token'] = $access_token;
	        }
	        if(!empty($access_token)){
	            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
	            $data['path'] = 'pages/data/data?id='.$code;
	            $data['scene'] = 'type=qrcode';//(string类型,必须是数字)
	            $data['width'] = 430;
	            $result = curlPost($url,$data,'POST');
	            // p($result);
				$filename = md5($code);  //文件名
	            $ret = file_put_contents('./uploads/'.$filename.'.png', $result, true);
				$path='./uploads/'.$filename.'.png';
				var_dump($path);
	            echo '成功';
	        }else{
	            echo 'string';
	        }
	 }
	function getJson($url,$data=array(),$method='GET'){
	        $ch = curl_init();//1.初始化  
	        curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址  
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
	        //4.参数如下  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
	        if($method=="POST"){//5.post方式的时候添加数据  
	            $data = json_encode($data);
	            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
	        }  
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        $output = curl_exec($ch);
	        curl_close($ch);
	        return json_decode($output, true);
	}
	
	function curlPost($url,$data,$method){
	        $ch = curl_init();   //1.初始化  
	        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
	        //4.参数如下  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
	        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_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
	            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
	          
	        if($method=="POST"){//5.post方式的时候添加数据  
	            $data = json_encode($data);
	            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
	        }  
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
	        $tmpInfo = curl_exec($ch);//6.执行  
	      
	        if (curl_errno($ch)) {//7.如果出错  
	            return curl_error($ch);  
	        }  
	        curl_close($ch);//8.关闭  
	        return $tmpInfo;  
	}  
	test(12);
?>

运行效果:so easy

猜你喜欢

转载自blog.csdn.net/qq_34761385/article/details/127557444