国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

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

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

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

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

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

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

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

?

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

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

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

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

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

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

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

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

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

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

?

創(chuàng)建公眾帳號(hào)后臺(tái)接口程序

創(chuàng)建一個(gè)Java Web工程,并新建一個(gè)能夠處理請(qǐng)求的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;

/**
 * 核心請(qǐng)求處理類(lèi)
 * 
 * @author liufeng
 * @date 2013-05-18
 */
public class CoreServlet extends HttpServlet {
	private static final long serialVersionUID = 4440739483644821986L;

	/**
	 * 確認(rèn)請(qǐng)求來(lái)自微信服務(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();
		// 通過(guò)檢驗(yàn)signature對(duì)請(qǐng)求進(jìn)行校驗(yàn),若校驗(yàn)成功則原樣返回echostr,表示接入成功,否則接入失敗
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
		out.close();
		out = null;
	}

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

}
可以看到,代碼中只完成了doGet方法,它的作用正是確認(rèn)請(qǐng)求是否來(lái)自于微信服務(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;

/**
 * 請(qǐng)求校驗(yàn)工具類(lèi)
 * 
 * @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對(duì)比,標(biāo)識(shí)該請(qǐng)求來(lái)源于微信
		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類(lèi)中的成員變量token,這里賦予什么值,在接口配置信息中的Token就要填寫(xiě)什么值,兩邊保持一致即可,沒(méi)有其他要求,建議用項(xiàng)目名稱(chēng)、公司名稱(chēng)縮寫(xiě)等,我在這里用的是項(xiàng)目名稱(chēng)weixinCourse。

最后再來(lái)看一下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的訪問(wèn)路徑 -->
	<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>
到這里,所有編碼都完成了,就是這么簡(jiǎn)單。接下來(lái)就是將工程發(fā)布到公網(wǎng)服務(wù)器上,如果沒(méi)有公網(wǎng)服務(wù)器環(huán)境,可以去了解下BAE、SAE或阿里云。發(fā)布到服務(wù)器上后,我們?cè)跒g覽器里訪問(wèn)CoreServlet,如果看到如下界面就表示我們的代碼沒(méi)有問(wèn)題:

?


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

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

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

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

?

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

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

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


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)

The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) Apr 21, 2025 pm 09:33 PM

The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

TikTok web version entrance login link address https TikTok web version entrance website free TikTok web version entrance login link address https TikTok web version entrance website free May 22, 2025 pm 04:24 PM

The login portal for the Douyin web version is https://www.douyin.com/. The login steps include: 1. Open the browser; 2. Enter the URL https://www.douyin.com/; 3. Click the "Login" button and select the login method; 4. Enter the account password; 5. Complete login. The web version provides functions such as browsing, searching, interaction, uploading videos and personal homepage management, and has advantages such as large-screen experience, multi-tasking, convenient account management and data statistics.

Are these C2C transactions in Binance risky? Are these C2C transactions in Binance risky? Apr 30, 2025 pm 06:54 PM

Binance C2C transactions allow users to buy and sell cryptocurrencies directly, and pay attention to the risks of counterparty, payment and price fluctuations. Choosing high-credit users and secure payment methods can reduce risks.

gate.io sesame door latest official app address gate.io sesame door latest official app address Apr 22, 2025 pm 01:03 PM

The official Gate.io APP can be downloaded in the following ways: 1. Visit the official website gate.io to download; 2. Search "Gate.io" on the App Store or Google Play to download. Be sure to download it through the official channel to ensure safety.

Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers May 22, 2025 pm 08:33 PM

Choosing UC browser or QQ browser depends on your needs: 1. UC browser is suitable for users who pursue fast loading and rich entertainment functions; 2. QQ browser is suitable for users who need stability and seamless connection with Tencent products.

Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Jun 05, 2025 pm 04:12 PM

Copying comics is undoubtedly a treasure that cannot be missed. Here you can find basketball comics in various styles, from passionate and inspiring competitive stories to relaxed and humorous daily comedy. Whether you want to relive the classics or discover new works, copying comics can meet your needs. Through the authentic online reading portal provided by copy comics, you will bid farewell to the trouble of pirated resources, enjoy a high-definition and smooth reading experience, and can support your favorite comic authors and contribute to the development of authentic comics.

Top 10 AI writing software rankings Recommended Which AI writing software is free Top 10 AI writing software rankings Recommended Which AI writing software is free Jun 04, 2025 pm 03:27 PM

Combining the latest industry trends and multi-dimensional evaluation data in 2025, the following are the top ten comprehensive AI writing software recommendations, covering mainstream scenarios such as general creation, academic research, and commercial marketing, while taking into account Chinese optimization and localization services:

Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Jun 05, 2025 pm 04:18 PM

Here, you can enjoy the vast ocean of comics and explore works of various themes and styles, from passionate young man comics to delicate and moving girl comics, from suspenseful and brain-burning mystery comics to relaxed and funny daily comics, there is everything, and there is always one that can touch your heartstrings. We not only have a large amount of genuine comic resources, but also constantly introduce and update the latest works to ensure that you can read your favorite comics as soon as possible.

See all articles