亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 微信公眾帳號開發(fā)教程第3篇-開發(fā)模式啟用及接口配置

微信公眾帳號開發(fā)教程第3篇-開發(fā)模式啟用及接口配置

Jun 13, 2016 am 11:28 AM
and enable develop WeChat success interface Tutorial model Apply edit Configuration

編輯模式與開發(fā)模式

微信公眾帳號申請成功后,要想接收處理用戶的請求,就必須要在“高級功能”里進(jìn)行配置,點(diǎn)擊“高級功能”,將看到如下界面:

從上圖中可以看到,高級功能包含兩種模式:編輯模式和開發(fā)模式,并且這兩種模式是互斥關(guān)系,即兩種模式不能同時(shí)開啟。那兩種模式有什么區(qū)別呢?作為開發(fā)人員到底要開啟哪一種呢?

編輯模式:主要針對非編程人員及信息發(fā)布類公眾帳號使用。開啟該模式后,可以方便地通過界面配置“自定義菜單”和“自動回復(fù)的消息”。

開發(fā)模式:主要針對具備開發(fā)能力的人使用。開啟該模式后,能夠使用微信公眾平臺開放的接口,通過編程方式實(shí)現(xiàn)自定義菜單的創(chuàng)建、用戶消息的接收/處理/響應(yīng)。這種模式更加靈活,建議有開發(fā)能力的公司或個(gè)人都采用該模式。

?

啟用開發(fā)模式(上)

微信公眾帳號注冊完成后,默認(rèn)開啟的是編輯模式。那么該如何開啟開發(fā)模式呢?操作步驟如下:

1)點(diǎn)擊進(jìn)入編輯模式,將右上角的編輯模式開關(guān)由“開啟”切換到“關(guān)閉”,如下圖所示:

2)點(diǎn)擊高級功能進(jìn)入到開發(fā)模式,將右上角的開發(fā)模式開關(guān)由“關(guān)閉”切換到“開啟”,但在切換時(shí)會遇到如下提示:

提示需要我們先成為開發(fā)者,才能開啟開發(fā)模式。那就先點(diǎn)擊下圖所示的“成為開發(fā)者”按鈕:

如果提示資料不全,那就先補(bǔ)齊資料再回來繼續(xù)操作。需要補(bǔ)全的資料有公眾帳號頭像、描述和運(yùn)營地區(qū)。

待資料補(bǔ)全后,再次點(diǎn)擊“成為開發(fā)者”,這時(shí)將看到接口配置信息界面,如下圖所示:

這里需要填寫URL和Token兩個(gè)值。URL指的是能夠接收處理微信服務(wù)器發(fā)送的GET/POST請求的地址,并且是已經(jīng)存在的,現(xiàn)在就能夠在瀏覽器訪問到的地址,這就要求我們先把公眾帳號后臺處理程序開發(fā)好(至少應(yīng)該完成了對GET請求的處理)并部署在公網(wǎng)服務(wù)器上。Token后面會詳細(xì)說明。

也就是說要完成接口配置,只需要先完成微信服務(wù)器的GET請求處理就可以?是的。 那這是為什么呢?因?yàn)檫@是微信公眾平臺接口中定義的。具體請參考API文檔-消息接口-消息接口指南中的網(wǎng)址接入部分。點(diǎn)此進(jìn)入。

上面寫的很清楚,其實(shí)你只要能理解上面在說什么就OK了,至于怎么編寫相關(guān)代碼,我已經(jīng)幫你完成了,請繼續(xù)往下看。

?

創(chuàng)建公眾帳號后臺接口程序

創(chuàng)建一個(gè)Java Web工程,并新建一個(gè)能夠處理請求的Servlet,命名任意,我在這里將其命名為org.liufeng.course.servlet.CoreServlet,代碼如下:

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;

	/**
	 * 確認(rèn)請求來自微信服務(wù)器
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 微信加密簽名
		String signature = request.getParameter("signature");
		// 時(shí)間戳
		String timestamp = request.getParameter("timestamp");
		// 隨機(jī)數(shù)
		String nonce = request.getParameter("nonce");
		// 隨機(jī)字符串
		String echostr = request.getParameter("echostr");

		PrintWriter out = response.getWriter();
		// 通過檢驗(yàn)signature對請求進(jìn)行校驗(yàn),若校驗(yàn)成功則原樣返回echostr,表示接入成功,否則接入失敗
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
		out.close();
		out = null;
	}

	/**
	 * 處理微信服務(wù)器發(fā)來的消息
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO 消息的接收、處理、響應(yīng)
	}

}
可以看到,代碼中只完成了doGet方法,它的作用正是確認(rèn)請求是否來自于微信服務(wù)器;而doPost方法不是我們這次要講的內(nèi)容,并且完成接口配置也不需要管doPost方法,就先空在那里。

在doGet方法中調(diào)用了org.liufeng.course.util.SignUtil.checkSignature方法,SignUtil.java的實(shí)現(xiàn)如下:

package org.liufeng.course.util;

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

/**
 * 請求校驗(yàn)工具類
 * 
 * @author liufeng
 * @date 2013-05-18
 */
public class SignUtil {
	// 與接口配置信息中的Token要一致
	private static String token = "weixinCourse";

	/**
	 * 驗(yàn)證簽名
	 * 
	 * @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三個(gè)參數(shù)進(jìn)行字典序排序
		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");
			// 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密
			byte[] digest = md.digest(content.toString().getBytes());
			tmpStr = byteToStr(digest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		content = null;
		// 將sha1加密后的字符串可與signature對比,標(biāo)識該請求來源于微信
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
	}

	/**
	 * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
	 * 
	 * @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;
	}

	/**
	 * 將字節(jié)轉(zhuǎn)換為十六進(jìn)制字符串
	 * 
	 * @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;
	}
}
這里唯一需要注意的就是SignUtil類中的成員變量token,這里賦予什么值,在接口配置信息中的Token就要填寫什么值,兩邊保持一致即可,沒有其他要求,建議用項(xiàng)目名稱、公司名稱縮寫等,我在這里用的是項(xiàng)目名稱weixinCourse。

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>coreServlet</servlet-name>
		<servlet-class>
			org.liufeng.course.servlet.CoreServlet
		</servlet-class>
	</servlet>

	<!-- url-pattern中配置的/coreServlet用于指定該Servlet的訪問路徑 -->
	<servlet-mapping>
		<servlet-name>coreServlet</servlet-name>
		<url-pattern>/coreServlet</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
到這里,所有編碼都完成了,就是這么簡單。接下來就是將工程發(fā)布到公網(wǎng)服務(wù)器上,如果沒有公網(wǎng)服務(wù)器環(huán)境,可以去了解下BAE、SAE或阿里云。發(fā)布到服務(wù)器上后,我們在瀏覽器里訪問CoreServlet,如果看到如下界面就表示我們的代碼沒有問題:

?


啊,代碼都報(bào)空指針異常了還說證明沒問題?那當(dāng)然了,因?yàn)橹苯釉诘刂窓谠L問coreServlet,就相當(dāng)于提交的是GET請求,而我們什么參數(shù)都沒有傳,在驗(yàn)證的時(shí)候當(dāng)然會報(bào)空指針異常。

接下來,把coreServlet的訪問路徑拷貝下來,再回到微信公眾平臺的接入配置信息界面,將coreServlet的訪問路徑粘貼到URL中,并將SignUtil類中指定的token值weixinCourse填入到Token中,填寫后的結(jié)果如下圖所示:

我在寫這篇教程的時(shí)候是使用的BAE環(huán)境,如果想學(xué)習(xí)微信公眾帳號開發(fā)又沒有公網(wǎng)服務(wù)器環(huán)境的,建議可以試試,注冊使用都很方便,如果有問題我們還可以交流。

接著點(diǎn)擊“提交”,如果程序?qū)懙臎]問題,并且URL、Token都填寫正確,可以在頁面最上方看到“提交成功”的提示,并會再次跳轉(zhuǎn)到開發(fā)模式設(shè)置界面,而且能夠看到“你已成為開發(fā)者”的提示,如下圖所示:

?

啟用開發(fā)模式(下)

這個(gè)時(shí)候就已經(jīng)成為開發(fā)者了,百般周折啊,哈哈,到這里還沒有完哦,還有最后一步工作就是將開發(fā)模式開啟。將右上角的開發(fā)模式開關(guān)由“關(guān)閉”切換到“開啟”,如下圖所示:

到這里,接口配置、開發(fā)模式的開啟就都完成了,本章節(jié)的內(nèi)容也就講到這里。接下來要章節(jié)要講的就是如何接收、處理、響應(yīng)由微信服務(wù)器轉(zhuǎn)發(fā)的用戶發(fā)送給公眾帳號的消息,也就是完成CoreServlet中doPost方法的編寫。


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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Jun 12, 2025 pm 08:18 PM

Nice Comics, an immersive reading experience platform dedicated to creating for comic lovers, brings together a large number of high-quality comic resources at home and abroad. It is not only a comic reading platform, but also a community that connects comic artists and readers and shares comic culture. Through simple and intuitive interface design and powerful search functions, NES Comics allows you to easily find your favorite works and enjoy a smooth and comfortable reading experience. Say goodbye to the long waiting and tedious operations, enter the world of Nice comics immediately and start your comic journey!

How to download Huobi on Android phones? Huobi download tutorial (step-by-step tutorial) How to download Huobi on Android phones? Huobi download tutorial (step-by-step tutorial) Jun 12, 2025 pm 10:12 PM

Android mobile phone users can download and install Huobi/Huobi App through the following steps: 1. Ensure the network is stable and the storage space is sufficient; 2. Download the App through Huobi/Huobi official website, use the browser to access the official website and click the download link or scan the QR code, or search and download through third-party application stores such as AppTreasure and Huawei App Market, and you can also obtain the installation package through friends' sharing; 3. Find the downloaded .apk file, enable the "Unknown Source App" installation permission, follow the prompts to complete the installation, etc.

Yiou Exchange Download and Installation Pack Yiou Android Download and Installation Pack Yiou Exchange Download and Installation Pack Yiou Android Download and Installation Pack Jun 12, 2025 pm 10:09 PM

The steps for downloading and installing the Yiou Exchange (OKX) Android client are as follows: 1. Download the official genuine installation package through the official website www.okx.com or the official QR code; 2. Find the downloaded .apk file in the mobile phone file manager and enable the "Unknown Source" installation permission; 3. Click the installation package to install, and after the installation is completed, open the APP and register or log in to the account; 4. Set up complex passwords, enable secondary verification, regularly change passwords, properly keep private keys and mnemonics, and beware of phishing websites to ensure account security.

Can I use WeChat on two phones at the same time? Can I use WeChat on two phones at the same time? Jul 11, 2025 am 03:28 AM

Yes, but there are restrictions. ① You can log in to the same account on both iPhone and Android phones, but logging in to the latest device will cause the earliest session to be offline; ② You can log in at the same time on the mobile phone and the computer desktop, but the functions are not synchronized; ③ Although using third-party tools or dual-app functions can enable logging in between two mobile phones, it is unofficially supported and may violate regulations; ④ Alternative solutions include using web version/desktop version to match the main phone, or transferring chat records through cloud backup and file tools. Some Android machines can also use "dual applications" to run two account instances.

Huobi v10.52.0 official Android version Huobi Android version download tutorial Huobi v10.52.0 official Android version Huobi Android version download tutorial Jun 18, 2025 pm 07:33 PM

Huobi App is the world's leading digital asset trading platform, providing safe, convenient and professional trading services. As a platform trusted by millions of users around the world, Huobi App supports transactions of various mainstream digital currencies such as Bitcoin and Ethereum, and provides a variety of trading tools such as spot, contracts, and leverage. The latest version of v10.52.0 optimizes the trading engine, improves speed and stability, adds a variety of new trading functions, and strengthens security protection.

What are interfaces in Go, and how do I define them? What are interfaces in Go, and how do I define them? Jun 22, 2025 pm 03:41 PM

In Go, an interface is a type that defines behavior without specifying implementation. An interface consists of method signatures, and any type that implements these methods automatically satisfy the interface. For example, if you define a Speaker interface that contains the Speak() method, all types that implement the method can be considered Speaker. Interfaces are suitable for writing common functions, abstract implementation details, and using mock objects in testing. Defining an interface uses the interface keyword and lists method signatures, without explicitly declaring the type to implement the interface. Common use cases include logs, formatting, abstractions of different databases or services, and notification systems. For example, both Dog and Robot types can implement Speak methods and pass them to the same Anno

Why won't Apache start after a configuration change? Why won't Apache start after a configuration change? Jun 19, 2025 am 12:05 AM

Apachenotstartingafteraconfigurationchangeisusuallycausedbysyntaxerrors,misconfigurations,orruntimeissues.(1)First,checktheconfigurationsyntaxusingapachectlconfigtestorhttpd-t,whichwillidentifyanytypos,incorrectpaths,orunclosedblockslikeor.(2)Next,re

Huobi (HTX) latest app download method: Apple/Android universal installation package obtain tutorial Huobi (HTX) latest app download method: Apple/Android universal installation package obtain tutorial Jun 18, 2025 pm 08:00 PM

HTX (formerly Huobi) launches the latest mobile app, supports Apple and Android systems, and provides real-time market trends, transactions, contract financial management and other functions. Users can download and install them through the official website, TestFlight or the app store.

See all articles