Home > WeChat Applet > WeChat Development > Three steps to teach you how to access WeChat public platform development

Three steps to teach you how to access WeChat public platform development

Y2J
Release: 2017-05-16 10:45:39
Original
2138 people have browsed it

Step 1: Fill in the server configuration

After logging in to the official website of the WeChat public platform, on the public platform backend management page - Developer Center page, click the "Modify Configuration" button and fill in the server Address (URL), Token and EncodingAESKey, where URL is the interfaceURL used by developers to receive WeChat messages and events. 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 the message encryption and decryption methods: plaintext mode, compatibility mode and security mode. The mode selection and server configuration will take effect immediately after submission. Developers are advised to fill in and select carefully. The default state of the encryption and decryption method is plain text mode. Selecting compatibility mode and security mode requires configuring the relevant encryption and decryption codes in advance. For details, please refer to the documentation of the message body signature and encryption and decryption sections.

Three steps to teach you how to access WeChat public platform development

Step 2: Verify the validity of the server address

After the developer submits the information, the WeChat server will send a GET request to the filled in server address URL , the GET request carries four parameters:

Three steps to teach you how to access WeChat public platform development

The developer verifies the request by checking the signature (the verification method is below). 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.

The encryption/verification process is as follows:

1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
Copy after login

PHP sample code for verifying signature:

private function checkSignature()
{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
Copy after login

PHP sample code download: Download

Step 3: Implement business logic according to the interface document

After successfully verifying the validity of the URL, the access will take effect and you will become a developer. If the public account type is a service account (subscription accounts can only use ordinary message interfaces), you can apply for certification on the public platform website. The successfully certified service account will obtain many interface permissions to meet the needs of developers.

Every time the user sends a message to the public account or generates a custom menu click event, the server configuration URL filled in by the developer will be pushed by the WeChat server Messages and events, and then developers can respond according to their own business logic, such as replying to messages, etc.

When the public account calls each interface, it will generally obtain the correct result. The specific results can be found in the description of the corresponding interface. When an error is returned, you can querythe cause of the error based on the return code. Global return code description

When a user sends a message to an official account, the sender of the message received by the official account is an OpenID, which is the result of encrypting the user's WeChat account. Each user has an OpenID for each official account. A unique OpenID.

In addition, since developers often need to share user accounts and unify account systems across multiple platforms (mobile applications, websites, public accounts), the WeChat open platform (open.weixin.qq.com) provides UnionID mechanism. Developers can obtain basic user information through OpenID. If the developer has multiple applications (mobile applications, website applications and public accounts, the public account will only obtain the UnionID after it is bound to the WeChat open platform account). The uniqueness of the user can be distinguished by obtaining the UnionID in the user's basic information, because as long as there are mobile applications, website applications and public accounts under the same WeChat open platform account, the user's UnionID is unique. In other words, the same user has the same UnionID for different applications under the same WeChat open platform account. For details, please view the Resource Center of the WeChat Open Platform - Mobile Application Development - WeChat Login - Authorization Relationship Interface Call Guide - Obtaining User Personal Information (UnionID Mechanism).

Please also note that WeChat public account interface only supports interface 80.

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2. WeChat public account platform source code download

3. Alizi order system source code download

The above is the detailed content of Three steps to teach you how to access WeChat public platform development. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template