Home > Backend Development > PHP Tutorial > WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

PHP中文网
Release: 2023-02-28 19:28:01
Original
1296 people have browsed it

Edit mode and development mode

WeChat public account application successful Finally, if you want to receive and process user requests, you must configure it in "Advanced Functions". Click "Advanced Functions" and you will see the following interface:

As you can see from the picture above, advanced functions include two modes: editing mode and development mode, and these two modes are mutually exclusive, that is, the two modes cannot be turned on at the same time. What is the difference between the two modes. ? Which one should you enable as a developer?

Editing mode: It is mainly used by non-programming public accounts. After turning on this mode, you can easily access it through the interface. Configure "Custom menu" and "Auto-reply messages".

Development mode: Mainly for people with development capabilities. After turning on this mode, you can use WeChat. The open interface of the public platform enables the creation of custom menus and the reception/processing/response of user messages through programming. This model is more flexible, and it is recommended that companies or individuals with development capabilities adopt this model.

Enable development mode (Part 1)

After the WeChat public account registration is completed, the editing mode is enabled by default. So how to enable the development mode? The steps are as follows:

1) Click to enter the editing mode, and switch the editing mode switch in the upper right corner from "on" to "off", as shown in the figure below:

2) Click Advanced Functions to enter the development mode, and switch the development mode switch in the upper right corner from "off" to "on", but you will encounter the following prompt when switching:

prompts that we need to become a developer before we can turn on the development mode. Then click the "Become a Developer" button as shown below:

If the information is incomplete, please complete the information first and then come back to continue the operation. The information that needs to be completed includes the public account avatar, description and operating area. 🎜>

After completing the information, click "Become a Developer" again, and you will see the interface configuration information interface, as shown below:

You need to fill in the URL and Token values ​​here. The URL refers to the address that can receive and process the GET/POST request

sent by the WeChat server, and it already exists. You can do it now. In the address accessed by the browser, this requires us to first develop the public account background processing program (at least it should have completed the processing of GET requests) and deploy it on the public network server. Token will be explained in detail later.

In other words, to complete the interface configuration, you only need to complete the GET request processing of the WeChat server first? Yes. So why is this? Because this is defined in the WeChat public platform interface. For details, please refer to the URL access section in the API

Documentation - Message Interface - Message Interface Guide. Click here to enter.

The above is very clear. In fact, as long as you can understand what it is saying, it will be OK. As for how to write the relevant code, I have already done it for you, please continue. Look down.

Create a public account backend interface program

Create a Java Web project and create a new

Servlet

that can handle requests. Name it whatever you want. I am Here it is named org.liufeng.course.servlet.CoreServlet, and the code is as follows:

As you can see, only the doGet method is completed in the code, and its function is to confirm whether the request comes from WeChat Server; and the doPost method is not what we are going to talk about this time, and there is no need to control the doPost method to complete the interface configuration, so just leave it empty for now.

package org.liufeng.course.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.liufeng.course.util.SignUtil;

/**
 * 核心请求处理类
 * 
 * @author liufeng
 * @date 2013-05-18
 */
public class CoreServlet extends HttpServlet {
	private static final long serialVersionUID = 4440739483644821986L;

	/**
	 * 确认请求来自微信服务器
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");

		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
		out.close();
		out = null;
	}

	/**
	 * 处理微信服务器发来的消息
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO 消息的接收、处理、响应
	}

}
Copy after login

The org.liufeng.course.util.SignUtil.checkSignature method is called in the doGet method. The implementation of SignUtil.java is as follows:

The only requirement here is What you need to pay attention to is the member

variable
package org.liufeng.course.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * 请求校验工具类
 * 
 * @author liufeng
 * @date 2013-05-18
 */
public class SignUtil {
	// 与接口配置信息中的Token要一致
	private static String token = "weixinCourse";

	/**
	 * 验证签名
	 * 
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @return
	 */
	public static boolean checkSignature(String signature, String timestamp, String nonce) {
		String[] arr = new String[] { token, timestamp, nonce };
		// 将token、timestamp、nonce三个参数进行字典序排序
		Arrays.sort(arr);
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		MessageDigest md = null;
		String tmpStr = null;

		try {
			md = MessageDigest.getInstance("SHA-1");
			// 将三个参数字符串拼接成一个字符串进行sha1加密
			byte[] digest = md.digest(content.toString().getBytes());
			tmpStr = byteToStr(digest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		content = null;
		// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
	}

	/**
	 * 将字节数组转换为十六进制字符串
	 * 
	 * @param byteArray
	 * @return
	 */
	private static String byteToStr(byte[] byteArray) {
		String strDigest = "";
		for (int i = 0; i < byteArray.length; i++) {
			strDigest += byteToHexStr(byteArray[i]);
		}
		return strDigest;
	}

	/**
	 * 将字节转换为十六进制字符串
	 * 
	 * @param mByte
	 * @return
	 */
	private static String byteToHexStr(byte mByte) {
		char[] Digit = { &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39; };
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
		tempArr[1] = Digit[mByte & 0X0F];

		String s = new String(tempArr);
		return s;
	}
}
Copy after login
token in the SignUtil class. Whatever value is assigned here, you must fill in the value for Token in the interface configuration information. Just keep both sides consistent. There are no other requirements. It is recommended to use the project name, Company name abbreviation, etc. I use the project name weixinCourse here.

最后再来看一下web.xml中,CoreServlet是怎么配置的,web.xml中的配置代码如下:

coreServlet
			org.liufeng.course.servlet.CoreServlet		coreServlet/coreServletindex.jsp
Copy after login

到这里,所有编码都完成了,就是这么简单。接下来就是将工程发布到公网服务器上,如果没有公网服务器环境,可以去了解下BAE、SAE或阿里云。发布到服务器上后,我们在浏览器里访问CoreServlet,如果看到如下界面就表示我们的代码没有问题:

 

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration
啊,代码都报空指针异常了还说证明没问题?那当然了,因为直接在地址栏访问coreServlet,就相当于提交的是GET请求,而我们什么参数都没有传,在验证的时候当然会报空指针异常。

接下来,把coreServlet的访问路径拷贝下来,再回到微信公众平台的接入配置信息界面,将coreServlet的访问路径粘贴到URL中,并将SignUtil类中指定的token值weixinCourse填入到Token中,填写后的结果如下图所示:

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

我在写这篇教程的时候是使用的BAE环境,如果想学习微信公众帐号开发又没有公网服务器环境的,建议可以试试,注册使用都很方便,如果有问题我们还可以交流。

接着点击“提交”,如果程序写的没问题,并且URL、Token都填写正确,可以在页面最上方看到“提交成功”的提示,并会再次跳转到开发模式设置界面,而且能够看到“你已成为开发者”的提示,如下图所示:

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

 

启用开发模式(下)

这个时候就已经成为开发者了,百般周折啊,哈哈,到这里还没有完哦,还有最后一步工作就是将开发模式开启。将右上角的开发模式开关由“关闭”切换到“开启”,如下图所示:

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

WeChat public account development tutorial Part 3 - Development mode activation and interface configuration

到这里,接口配置、开发模式的开启就都完成了,本章节的内容也就讲到这里。接下来要章节要讲的就是如何接收、处理、响应由微信服务器转发的用户发送给公众帐号的消息,也就是完成CoreServlet中doPost方法的编写。

 以上就是微信公众帐号开发教程第3篇-开发模式启用及接口配置的内容,更多相关内容请关注PHP中文网(www.php.cn)!

http://www.bkjia.com/PHPjc/444575.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444575.htmlTechArticle编辑模式与开发模式 微信公众帐号申请成功后,要想接收处理用户的请求,就必须要在高级功能里进行配置,点击高级功能,将看到如下界...


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template