JIYIK CN >

Current Location:Home > Learning > NETWORK >

WeChat public account development tutorial interface configuration and identity authentication

Author:JIYIK Last Updated:2025/03/16 Views:

To become a developer of WeChat official account, you must first have a server that can be accessed externally. The development of WeChat official account does not limit the use of any language. Here we use PHP as the development language.

Interface access configuration

After logging in to the WeChat public platform official website, click the "Modify Configuration" button on the public platform backend management page - Developer Center page , and fill in the server address (URL), Token, and EncodingAESKey, where the URL is the interface URL used by developers to receive WeChat messages and events. The Token can be filled in arbitrarily by the developer and used to generate the signature (the Token will be compared with the Token contained in the interface URL to verify security). The EncodingAESKey is filled in manually by the developer or randomly generated, and will be used as the encryption and decryption key for the message body.

At the same time, developers can choose the message encryption and decryption mode: plain text mode, compatible mode and secure mode. The mode selection and server configuration will take effect immediately after submission. Developers are requested to fill in and select carefully. The default state of the encryption and decryption mode is plain text mode. Selecting compatible mode and secure mode requires configuring the relevant encryption and decryption code in advance. For details, please refer to the document on message body signature and encryption and decryption.

Authentication

After filling in the interface information, we start to authenticate the identity and verify the validity of the server. After we submit the information, the WeChat server will send a GET request to the URL we filled in above. In the URL address filled in above, we can see that we filled in a PHP file. Yes, we use this PHP file for verification.

Some verification codes are as follows

public function valid(){
     $echoStr = $_GET["echostr"];
 //valid signature , option
     if($this->checkSignature()){
        echo $echoStr;
        exit;
     }
}
private function checkSignature(){
    // you must define TOKEN by yourself
    if (!defined("TOKEN")) {
        throw new Exception('TOKEN is not defined!');
    }
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
         $tmpArr = array($token, $timestamp, $nonce);
    // use SORT_STRING rule
         sort($tmpArr, SORT_STRING);
         $tmpStr = implode( $tmpArr );
         $tmpStr = sha1( $tmpStr );
         if( $tmpStr == $signature ){
                   return true;
         }else{
                   return false;
         }
}

From the above code we can see that the WeChat server will send four parameters to our server address, namely:

signature WeChat encrypted signature, signature combines 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

So where does the token come from? The token here is the field below the Token URL that we filled in above. So we need to define the token constant in our verification code

define("TOKEN","The token we filled in"); //Note that the value here must be consistent with what we filled in above

php complete code download

After our verification code is ready, submit the information, and we will be surprised to see that the configuration is successful at the top.

After the access is successful, we need to think about enabling our interface, and then we can achieve the desired effect according to the development documentation.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

What multipart/form-data does in post Upload upload files

Publish Date:2025/03/18 Views:63 Category:NETWORK

Everyone has used the attribute enctype="multipart/form-data" when uploading files using a form. What is the role of multipart/form-data? Let's talk about this topic. First, let's look at a case Look at the first code   form action= "handl

About application/x-www-form-urlencoded

Publish Date:2025/03/18 Views:147 Category:NETWORK

As a data format of form, application/x-www-form-urlencoded has its own characteristics   form action= "handle.php" method= "post"    input type= "text" name= " uname"   class= " uname" /br /    input type= "text" name= "email" class=

My understanding of webservice is this

Publish Date:2025/03/18 Views:147 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

WSDL looks like this

Publish Date:2025/03/18 Views:190 Category:NETWORK

When I first started learning Webservice, I found that there were quite a lot of knowledge points involved, and each point could be a school of its own. Especially when I saw WSDL, I looked up information for a long time, but I was still a

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:61 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

Implementing a group chat room using socket.io

Publish Date:2025/03/18 Views:65 Category:NETWORK

This article will share with you an example of using socket.io to realize the function of group chat. If you want to use socket.io, you must use nodejs to implement the server, so we need to install socket.io in nodejs Install socket.io How

First contact with CGI

Publish Date:2025/03/18 Views:51 Category:NETWORK

Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa

Getting started with FastCGI

Publish Date:2025/03/18 Views:164 Category:NETWORK

In "First Contact with CGI", we mentioned the operating mechanisms of CGI and Server APIs, as well as their respective advantages and disadvantages. In this chapter, we will learn about FastCGI, which combines the advantages of CGI and Serv

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial