微信公众号-添加参数获取场景二维码路径

1.微信公众平台测试账号

我用内网穿透把本地的项目映射到外网,主要为了方便测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在本地项目映射成功后可以扫一下“测试号二维码
”看是否正常进入到后台

2,添加参数获取场景二维码路径

首先通过上面得到的appID和appsecret获取到access_token

http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
可以得到返回结果
{“access_token”:“ACCESS_TOKEN”,“expires_in”:7200}

官方文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

再通过access_token获取场景二维码的url参数

临时二维码可以自定义时间和永久二维码的请求路径都是:
http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN
重要的是参数,这也是场景二维码和其它二维码的区别,也就是scene:
当场景二维码需要携带的参数是数字类型:
{“expire_seconds”: 604800, “action_name”: “QR_SCENE”, “action_info”: {“scene”: {“scene_id”: 123}}}
当场景二维码需要携带的参数是字符串类型:
{“expire_seconds”: 604800, “action_name”: “QR_STR_SCENE”, “action_info”: {“scene”: {“scene_str”: “abc”}}}

在这里插入图片描述
也就是说
QR_SCENE和QR_LIMIT_SCENE所匹配的是scene_id;
QR_STR_SCENE和QR_LIMIT_STR_SCENE所匹配的是scene_str;

可以得到返回值:
{“ticket”:“gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm
3sUw==”,“expire_seconds”:60,“url”:“http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI”}
其中ticket就是所需要的场景二维码的参数
在这里插入图片描述
官方文档:https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html

通过 ticket 换取二维码
获取二维码 ticket 后,开发者可用 ticket 换取二维码图片。
HTTP GET请求:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET (TICKET需要UrlEncode)

3.后台处理

对于关注,取消关注,再次扫码,点击等事件场景二维码都会返回到指定的后台路径(固定二维码,只能触发特定的操作比:已经关注后再次扫码就会直接登录进去不会再触发跳转后台路径)

代码示例:

	// xml格式的消息数据
    String respXml = null;
    // 调用parseXml方法解析请求消息
     Map<String, String> requestMap = MessageUtil.parseXml(request);
     // 发送方帐号
     String fromUserName = requestMap.get("FromUserName");
     // 开发者微信号
     String toUserName = requestMap.get("ToUserName");
     // 消息类型
     String msgType = requestMap.get("MsgType");
     // 事件类型
     //SCAN:已绑定(再次扫码触发),SUBSCRIBE:绑定,UNSUBSCRIBE:取消订阅,CLICK:点击事件
     String eventType = requestMap.get("Event");
     //从二维码传递过来的用户信息
     //如果是第一次关注会在用户信息前默认加qrscene_
     String id = requestMap.get("EventKey").replace("qrscene_", "");

页面显示:

<img class="qrcode lightBorder" src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={{ticket}}" height="200" width="200"> 

请求获取ticket

package com.xxx.xxx.xxx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import net.sf.json.JSONObject;

@Controller
public class cs {
	public static void main(String[] args) throws Exception {
		Map<String, String> parameters1 = new HashMap<String, String>();
		parameters1.put("grant_type", "client_credential");
		parameters1.put("appid", "wx293af304e9325ff4");
		parameters1.put("secret", "e3f569eab65224d7a360bce5648477bd");
		String result1 = sendGet("https://api.weixin.qq.com/cgi-bin/token", parameters1);
		JSONObject fromObject = JSONObject.fromObject(result1.toString());
		String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + fromObject.get("access_token");
		String sendPost = doPost(url,
				"{\"expire_seconds\": 604800,\"action_name\":\"QR_STR_SCENE\",\"action_info\":{\"scene\":{\"scene_str\": \"0077ae677f6846bf9ee2fd31b2811312\"}}}");
		System.out.println(sendPost);
	}
	/**
	 * 发送POST请求
	 * @param url目的地址
	 * @param parameters请求参数,String类型。
	 * @return 远程响应结果
	 */
	public static String doPost(String url, String params) throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);// 创建httpPost
		httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
		httpPost.setHeader("Accept", "application/json");
		httpPost.setHeader("Content-Type", "application/json");
		String charSet = "UTF-8";
		StringEntity entity = new StringEntity(params, charSet);
		httpPost.setEntity(entity);
		CloseableHttpResponse response = null;
		try {
			response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity);
				return jsonString;
			} else {
				System.err.println("请求返回:" + state + "(" + url + ")");
			}
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 发送GET请求
	 * @param url目的地址
	 * @param parameters请求参数,Map类型。
	 * @return 远程响应结果
	 */
	public static String sendGet(String url, Map<String, String> parameters) {
		String result = "";// 返回的结果
		BufferedReader in = null;// 读取响应输入流
		StringBuffer sb = new StringBuffer();// 存储参数
		String params = "";// 编码之后的参数
		try {
			// 编码请求参数
			if (parameters.size() == 1) {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(parameters.get(name));
				}
				params = sb.toString();
			} else {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(parameters.get(name)).append("&");
				}
				String temp_params = sb.toString();
				params = temp_params.substring(0, temp_params.length() - 1);
			}
			String full_url = url + "?" + params;
			// 创建URL对象
			java.net.URL connURL = new java.net.URL(full_url);
			// 打开URL连接
			java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
			// 设置通用属性
			httpConn.setRequestProperty("Accept", "*/*");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
			// 建立实际的连接
			httpConn.connect();
			// 响应头部获取
			Map<String, List<String>> headers = httpConn.getHeaderFields();
			// 遍历所有的响应头字段
			for (String key : headers.keySet()) {
				System.out.println(key + "\t:\t" + headers.get(key));
			}
			// 定义BufferedReader输入流来读取URL的响应,并设置编码方式
			in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
			String line;
			// 读取返回的内容
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
}
 /** 
     * 发送POST请求 
     * @param url 目的地址 
     * @param parameters 请求参数,Map类型。 
     * @return 远程响应结果 
     */  
    public static String sendPost(String url, Map<String, String> parameters) {
        String result = "";// 返回的结果  
        BufferedReader in = null;// 读取响应输入流  
        PrintWriter out = null;  
        StringBuffer sb = new StringBuffer();// 处理请求参数  
        String params = "";// 编码之后的参数  
        try {
            // 编码请求参数  
            if (parameters.size() == 1) {  
                for (String name : parameters.keySet()) {
					String aa=parameters.get(name);
					sb.append(name).append("=").append(parameters.get(name)).append("&");
                }  
                params = sb.toString();  
            } else {  
                for (String name : parameters.keySet()) {
                	String aa=parameters.get(name);
                    sb.append(name).append("=").append(parameters.get(name)).append("&");
                }
                String temp_params = sb.toString();
                params = temp_params.substring(0, temp_params.length() - 1);
            }
            // 创建URL对象  
            java.net.URL connURL = new java.net.URL(url);  
            // 打开URL连接  
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL  
                    .openConnection();  
            // 设置通用属性  
            httpConn.setRequestProperty("Accept", "*/*");  
            httpConn.setRequestProperty("Connection", "Keep-Alive");  
            httpConn.setRequestProperty("User-Agent",  
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");  
            // 设置POST方式  
            httpConn.setDoInput(true);  
            httpConn.setDoOutput(true);  
            // 获取HttpURLConnection对象对应的输出流  
            out = new PrintWriter(httpConn.getOutputStream());  
            // 发送请求参数  
            out.write(params);  
            // flush输出流的缓冲  
            out.flush();  
            // 定义BufferedReader输入流来读取URL的响应,设置编码方式  
            in = new BufferedReader(new InputStreamReader(httpConn  
                    .getInputStream(), "UTF-8"));  
            String line;  
            // 读取返回的内容  
            while ((line = in.readLine()) != null) {  
                result += line;  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (out != null) {  
                    out.close();  
                }  
                if (in != null) {  
                    in.close();  
                }  
            } catch (IOException ex) {  
                ex.printStackTrace();  
            }  
        }  
        return result;  
    }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您的问题是关于在PHP开发的微信公众号中,如何添加小程序菜单以及如何实现跳转小程序的问题,对吧? 首先,要在微信公众号添加小程序菜单,您需要在微信公众平台的开发者中心中,通过自定义菜单功能添加一个小程序菜单。具体步骤如下: 1. 登录微信公众平台开发者中心,进入“公众号设置”页面; 2. 在左侧菜单栏中选择“菜单设置”,然后点击“自定义菜单”; 3. 在自定义菜单页面中,点击“添加菜单”按钮; 4. 在添加菜单页面中,选择“小程序”类型; 5. 在小程序菜单设置页面中,填写小程序的AppID、小程序页面路径小程序的名称; 6. 点击“保存”按钮,即可完成小程序菜单的添加。 接下来,要实现在微信公众号中跳转小程序,您需要在PHP代码中调用微信公众号的JS-SDK,通过JS-SDK提供的API实现跳转小程序的功能。具体步骤如下: 1. 在PHP代码中引入微信公众号的JS-SDK文件; 2. 在PHP代码中调用微信公众号的JS-SDK初始化函数,传入微信公众号的AppID和当前页面的URL; 3. 在需要跳转小程序的地方,调用微信公众号的JS-SDK提供的API,传入小程序的AppID、小程序页面路径和其他参数(如是否要打开小程序的debug模式等); 4. 用户点击跳转小程序的按钮时,JS-SDK会自动调起微信客户端,并跳转到指定的小程序页面。 希望这个回答能够解决您的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

engr_chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值