Table of Contents
原理
开发
注意
Home php教程 php手册 微信服务号开发笔记

微信服务号开发笔记

Jun 06, 2016 pm 08:12 PM
principle develop WeChat Serve Compare notes Simple

原理 微信服务号的原理比较简单。从请求响应角度来看,逻辑是: 用户微信客户端 --- 微信服务器 --- 微信服务号后台程序 --- 数据库或Web Service 也就是,用户的各种请求先经过微信的服务器,微信服务器将请求转发给微信服务号后台程序。 既然是微信服务器

原理

微信服务号的原理比较简单。从请求响应角度来看,逻辑是:

用户微信客户端 微信服务器 微信服务号后台程序 数据库或Web Service

也就是,用户的各种请求先经过微信的服务器,微信服务器将请求转发给微信服务号后台程序。

既然是微信服务器把用户请求数据转发给我们开发的微信服务号后台程序,那么在启用服务号的开发模式时就需要提供一个URL。另外为了安全 考虑,还需要提供一个token,用来校验请求是否来自微信服务器。校验的方法见微信开发者文档。校验又分两种:

在开发者首次提交验证申请时,微信服务器将发送GET请求到填写的URL上,并且带上四个参数(signature、timestamp、nonce、echostr),开发者通过对签名(即signature)的效验,来判断此条消息的真实性。

此后,每次开发者接收用户消息的时候,微信也都会带上前面三个参数(signature、timestamp、nonce)访问开发者设置的URL,开发者依然通过对签名的效验判断此条消息的真实性。效验方式与首次提交验证申请一致。

微信服务器转发到微信服务号后台程序的消息以及服务号后台程序返回给微信服务器的响应,都是XML格式,消息中都会指明发送者和接收者。 请求消息中的发送者为微信用户的openid,接收者为服务号开发者微信号,响应消息则相反。

消息中还有一个关键字段MsgType指明消息类型。微信将请求消息分为:普通消息、事件推送、语音识别结果三大类,其中,

  • 普通消息分6种:文本、图片、语音、视频、地理位置、链接。
  • 事件推送分4种:关注/取消关注事件、扫描带参数二维码事件、上报地理位置事件、自定义菜单事件(点击菜单拉取消息时的事件推送、点击菜单跳转链接时的事件推送)

响应消息也分6种:文本、图片、语音、视频、音乐、图文。

工作中为产品开发的微信服务号,目前对于普通消息,服务号后台程序自动响应一段文本消息;对于事件推送中的关注事件,则是响应一段欢迎、 功能简介的文本消息。

另外我们使用了自定义菜单事件,要想使用该事件,先要为你的微信服务号提供自定义菜单。自定义菜单是通过微信提供的API向其推送的。

在通过API向微信服务器推送自定义菜单时,微信服务器需要确认是否为可信任请求,确认方法是基于请求参数access_token。 access_token需要通过API向微信服务器获取。成功启用服务号的开发模式后,微信会为服务号分配一个AppId和AppSecret,获取access_token时 需要带上这两个参数,用于微信服务器确认是否为已注册服务号的请求。access_token相关文档见这里。

当成功获取access_token后就可以通过API创建、删除、查询服务号的自定义菜单了。

自定义菜单相关文档见这里。

以下Python代码是对自定义菜单API的简单封装:

开发

对于微信服务号后台程序,当然可以参照微信官方文档,从头开始实现。但可用非官方微信公众号SDK也不少。比如我使用的PHP SDK是这个。

其他语言的SDK也可以找找看。

注意

  • 我在启用微信服务号的开发模式之后,就直接使用该服务号进行开发测试,但在开发测试过程中,已经有一些产品的用户关注了该服务号,那么这个过程中用户的体验会很差。正确的过程应该是先申请测试帐号进行开发测试,等开发测试完成后,再上线服务号

  • 自定义菜单在变更之后并不是实时更新的,官方文档的说明是:创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果

  • 微信客户端应该是基于浏览器内核技术的,所以能解释渲染网页元素。图文消息中图片其实是一个img元素,所以需要你自己提供图床。微信官方的公众平台提供的消息发送功能,对于图文消息要求必须带图片,但服务号后台程序响应的图文消息中其实可以留空图片链接,这样用户收到图文消息看到的效果也许就是你需要的。另外,文本消息也是可以带超链接a元素的。

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

There are rumors that 'iPhone 16 may not support WeChat', and Apple's technical consultant in China said that it is communicating with Tencent about app store commissions There are rumors that 'iPhone 16 may not support WeChat', and Apple's technical consultant in China said that it is communicating with Tencent about app store commissions Sep 02, 2024 pm 10:45 PM

Thanks to netizens Qing Qiechensi, HH_KK, Satomi Ishihara and Wu Yanzu of South China for submitting clues! According to news on September 2, there are recent rumors that "iPhone 16 may not support WeChat." In response to this, a reporter from Shell Finance called Apple's official hotline. Apple's technical consultant in China responded that whether iOS systems or Apple devices can continue to use WeChat, and WeChat The issue of whether it can continue to be listed and downloaded on the Apple App Store requires communication and discussion between Apple and Tencent to determine the future situation. Software App Store and WeChat Problem Description Software App Store technical consultant pointed out that developers may need to pay fees to put software on the Apple Store. After reaching a certain number of downloads, Apple will need to pay corresponding fees for subsequent downloads. Apple is actively communicating with Tencent,

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

People familiar with the matter responded that 'WeChat may not support Apple iPhone 16': Rumors are rumors People familiar with the matter responded that 'WeChat may not support Apple iPhone 16': Rumors are rumors Sep 02, 2024 pm 10:43 PM

Rumors of WeChat supporting iPhone 16 were debunked. Thanks to netizens Xi Chuang Jiu Shi and HH_KK for submitting clues! According to news on September 2, there are rumors today that WeChat may not support iPhone 16. Once the iPhone is upgraded to the iOS 18.2 system, it will not be able to use WeChat. According to "Daily Economic News", it was learned from people familiar with the matter that this rumor is a rumor. Apple's response: According to Shell Finance, Apple's technical consultant in China responded that the issue of whether WeChat can continue to be used on iOS systems or Apple devices, and whether WeChat can continue to be listed and downloaded in the Apple App Store, needs to be resolved between Apple and Tencent. Only through communication and discussion can we determine the future situation. Currently, Apple is actively communicating with Tencent to confirm whether Tencent will continue to

gateio Chinese official website gate.io trading platform website gateio Chinese official website gate.io trading platform website Feb 21, 2025 pm 03:06 PM

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

List of handling fees for okx trading platform List of handling fees for okx trading platform Feb 15, 2025 pm 03:09 PM

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

See all articles