The working principle of the WeChat public platform message interface can be roughly understood like this: the process from the user end to the public account end is like this. The user sends a message to the WeChat server, and the WeChat server will receive it. The message is posted to the URL filled in by the user when accessing. In the URL handler, the legitimacy of the message is first judged. After successful judgment, the corresponding response is made based on the content of the message body. The principle is easy to understand, and it may be easier for those who have been exposed to sockets.
However, WeChat’s documentation is sometimes a bit confusing, and the documentation on WeChat access is really not flattering. The first step in the official document is to "apply for a message interface". Here you need to fill in a URL. This URL is used to process messages sent by WeChat, but WeChat writes the relevant configuration in the URL in the second step. Be careful and you will screw up newbies. Below I will explain it according to my understanding.
First of all, when the developer accesses, the WeChat server will send a get request to the URL you filled in. This request carries four parameters, respectively signature (WeChat encrypted signature, signature combination The token parameter filled in by the developer and the timestamp parameter and nonce parameter in the request), timestamp (Timestamp), nonce (random number), echostr (random string). .Use HttpContext.Current.Request.RawUrl to obtain the original URL of the current request, as shown in the following figure:
The developer verifies the request by checking the signature (see below) verification method). If it is confirmed that this GET request comes from the WeChat server, please return the echostr parameter content as it is, then the access will take effect and you will become a developer successfully, otherwise the access will fail.
加密/校验流程如下:1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。下面是代码实现。首先,在你的处理程序中(我是新建了一个一般处理程序wx.ashx),判断当前请求的类型,因为接入时,是发送的GET请求,消息处理是发送的POST请求。如下图:这里我对验证url的方法进行封装。/// <summary> /// 验证url权限, 接入服务器 /// </summary> /// <param name="token"></param> /// <returns></returns> public static bool ValidUrl(string token) { string echoStr = VqiRequest.GetQueryString("echoStr"); if (CheckSignature(token)) { if (!string.IsNullOrEmpty(echoStr)) { Utils.ResponseWrite(echoStr); return true; } } return false; }/// <summary> /// 验证微信签名 /// </summary> /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// <returns></returns> public static bool CheckSignature(string token) { string signature = VqiRequest.GetQueryString("signature"); string timestamp = VqiRequest.GetQueryString("timestamp"); string nonce = VqiRequest.GetQueryString("nonce"); string[] ArrTmp = { token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = Utils.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } }Note: The code VqiRequest.GetQueryString is the encapsulated QueryString request method. When used, it can be changed to QueryString[""]
The processing process is as shown in the figure:
Put the three parameters signature, timestamp and nonce in the array,
Sorted ArrTmp:
Concatenate the three parameter strings into one string:
Encrypt tmpStr with sha1, and convert the encrypted string into lowercase:
Then compare it with the signature, if it is the same, it means Verification successful.
If the verification is successful, echoStr will be returned to WeChat.
/// <summary> /// 根据指定的密码和哈希算法生成一个适合于存储在配置文件中的哈希密码 /// </summary> /// <param name="str">要进行哈希运算的密码</param> /// <param name="type"> 要使用的哈希算法</param> /// <returns>经过哈希运算的密码</returns> public static string HashPasswordForStoringInConfigFile(string str, string type) { return FormsAuthentication.HashPasswordForStoringInConfigFile(str, type); } public static void ResponseWrite(string str) { HttpContext.Current.Response.Write(str); HttpContext.Current.Response.End(); }
After the handler is written, deploy it to iis (please refer to Tutorial 1 for the method), log in to the WeChat management background mp.weixin. qq.com/ On the developer center page, click the "Modify Configuration Button" to fill in the URL, Token and EncodingAESKey, where the URL is the interface URL used by developers to receive WeChat server data. The token can be filled in by the developer arbitrarily and used to generate a signature (the token will be compared with the token contained in the interface URL to verify the security). EncodingAESKey is manually filled in by the developer or randomly generated, and will be used as the message body encryption and decryption key. At the same time, developers can choose message encryption and decryption methods: plaintext mode, compatibility mode and security mode. The mode selection and server configuration will take effect immediately after submission. The default state of the encryption and decryption method is plaintext mode. The encryption and decryption modes will be shared with you in a later series, so stay tuned.
As shown in the picture:
##After clicking submit, the WeChat server Request get to the url written above. If the verification is successful, the binding is successful.
END [Related recommendations] 1.2.
The above is the detailed content of Newbie Access Guide to WeChat Development Department. For more information, please follow other related articles on the PHP Chinese website!

PHP是一种开源的脚本语言,广泛应用于Web开发和服务器端编程,尤其在微信开发中得到了广泛的应用。如今,越来越多的企业和开发者开始使用PHP进行微信开发,因为它成为了一款真正的易学易用的开发语言。在微信开发中,消息的加密和解密是一个非常重要的问题,因为它们涉及到数据的安全性。对于没有加密和解密方式的消息,黑客可以轻松获取到其中的数据,对用户造成威胁

在微信公众号开发中,用户标签管理是一个非常重要的功能,可以让开发者更好地了解和管理自己的用户。本篇文章将介绍如何使用PHP实现微信用户标签管理功能。一、获取微信用户openid在使用微信用户标签管理功能之前,我们首先需要获取用户的openid。在微信公众号开发中,通过用户授权的方式获取openid是比较常见的做法。在用户授权完成后,我们可以通过以下代码获取用

随着微信的普及,越来越多的企业开始将其作为营销工具。而微信群发功能,则是企业进行微信营销的重要手段之一。但是,如果只依靠手动发送,对于营销人员来说是一件极为费时费力的工作。所以,开发一款微信群发工具就显得尤为重要。本文将介绍如何使用PHP开发微信群发工具。一、准备工作开发微信群发工具,我们需要掌握以下几个技术点:PHP基础知识微信公众平台开发开发工具:Sub

微信是目前全球用户规模最大的社交平台之一,随着移动互联网的普及,越来越多的企业开始意识到微信营销的重要性。在进行微信营销时,客服服务是至关重要的一环。为了更好地管理客服聊天窗口,我们可以借助PHP语言进行微信开发。一、PHP微信开发简介PHP是一种开源的服务器端脚本语言,广泛运用于Web开发领域。结合微信公众平台提供的开发接口,我们可以使用PHP语言进行微信

随着微信成为了人们生活中越来越重要的一个通讯工具,其敏捷的消息传递功能迅速受到广大企业和个人的青睐。对于企业而言,将微信发展为一个营销平台已经成为趋势,而微信开发的重要性也逐渐凸显。在其中,群发功能更是被广泛使用,那么,作为PHP程序员,如何实现群发消息发送记录呢?下面将为大家简单介绍一下。1.了解微信公众号相关开发知识在了解如何实现群发消息发送记录之前,我

随着移动互联网的普及,微信作为一款社交软件,越来越多的人开始使用,并且微信开放平台也给开发者带来了众多的机会。近年来,随着人工智能技术的发展,语音识别技术逐渐成为了移动端开发的热门技术之一。在微信开发中,如何实现语音识别成为很多开发者关注的问题。本文将介绍如何利用PHP开发微信应用实现语音识别功能。一、语音识别原理在介绍如何实现语音识别之前,我们先了解一下语

ThinkPHP6微信开发指南:快速搭建微信公众号应用引言:微信公众号作为一种重要的社交媒体平台,为个人和企业在市场推广、信息传播等方面提供了很大的机会。在这篇文章中,我们将介绍如何使用ThinkPHP6快速搭建一个微信公众号应用,并且提供一些常用的代码示例。环境准备在开始开发之前,我们首先需要准备好以下环境:PHP7以上版本ThinkPHP6框架微信公众号

随着互联网和移动智能设备的发展,微信成为了社交和营销领域不可或缺的一部分。在这个越来越数字化的时代,如何使用PHP进行微信开发已经成为了很多开发者的关注点。本文主要介绍如何使用PHP进行微信开发的相关知识点,以及其中的一些技巧和注意事项。一、开发环境准备在进行微信开发之前,首先需要准备好相应的开发环境。具体来说,需要安装PHP的运行环境,以及微信公众平台提


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools