WeChat Development Access Guide

WeChat Development Access Guide

Overview

To access the WeChat public platform for development, developers need to follow the following steps:

1. Fill in the server configuration
2. Verify the validity of the server address
3. Implement business logic based on interface documentation

These three steps are described in detail below.
Step 1: Fill in the server 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.

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 server address URL filled in. The GET request carries four parameters:

The developer verifies the request by checking the signature (see below for verification methods). If it is confirmed that the GET request comes from the WeChat server, please return the echostr parameter content as is, then the access is effective and you become a developer successfully, otherwise the access fails.

The encryption/verification process is as follows:
1. Sort the three parameters token, timestamp, and nonce in lexicographic order
2. Concatenate the three parameter strings into one string for sha1 encryption
3. The developer can obtain the encrypted string and compare it with the signature to identify that the request comes from WeChat

PHP sample code for checking signature:

  1. private function checkSignature()
  2. {
  3. $signature = $_GET[ "signature" ];
  4. $timestamp = $_GET[ "timestamp" ];
  5. $nonce = $_GET[ "nonce" ];
  6.                  
  7. $token = TOKEN;
  8. $tmpArr = array($token, $timestamp, $nonce);
  9. sort($tmpArr, SORT_STRING);
  10. $tmpStr = implode( $tmpArr );
  11. $tmpStr = sha1( $tmpStr );
  12.      
  13. if ( $tmpStr == $signature ) {
  14. return   true ;
  15. } else {
  16. return   false ;
  17. }
  18. }

PHP Sample Code

  1. <?php
  2. /**
  3. * wechat php test
  4. */  
  5.  
  6. //define your token  
  7. define( "TOKEN" , "weixin" );
  8. $wechatObj = new wechatCallbackapiTest();
  9. $wechatObj- >valid();
  10.  
  11. class wechatCallbackapiTest
  12. {
  13. public   function valid()
  14. {
  15. $echoStr = $_GET [ "echostr" ];
  16.  
  17. //valid signature , option  
  18. if ( $this ->checkSignature()){
  19. echo   $echoStr ;
  20. exit ;
  21. }
  22. }
  23.  
  24. public   function responseMsg()
  25. {
  26. //get post data, May be due to the different environments  
  27. $postStr = $GLOBALS [ "HTTP_RAW_POST_DATA" ];
  28.  
  29. //extract post data  
  30. if (! empty empty ( $postStr )){
  31. /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
  32. the best way is to check the validity of xml by yourself */  
  33. libxml_disable_entity_loader(true);
  34. $postObj = simplexml_load_string( $postStr , 'SimpleXMLElement' , LIBXML_NOCDATA);
  35. $fromUsername = $postObj ->FromUserName;
  36. $toUsername = $postObj ->ToUserName;
  37. $keyword = trim( $postObj ->Content);
  38. $time = time();
  39. $textTpl = "<xml>
  40. <ToUserName><![CDATA[%s]]></ToUserName>
  41. <FromUserName><![CDATA[%s]]></FromUserName>
  42. <CreateTime>%s</CreateTime>
  43. <MsgType><![CDATA[%s]]></MsgType>
  44. <Content><![CDATA[%s]]></Content>
  45. <FuncFlag>0</FuncFlag>
  46. </xml>";
  47. if (! empty empty ( $keyword ))
  48. {
  49. $msgType = "text" ;
  50. $contentStr = "Welcome to wechat world!" ;
  51. $resultStr = sprintf( $textTpl , $fromUsername , $toUsername , $time , $msgType , $contentStr );
  52. echo   $resultStr ;
  53. } else {
  54. echo   "Input something..." ;
  55. }
  56.  
  57. } else {
  58. echo   "" ;
  59. exit ;
  60. }
  61. }
  62.          
  63. private   function checkSignature()
  64. {
  65. // you must define TOKEN by yourself  
  66. if (!defined( "TOKEN" )) {
  67. throw   new Exception( 'TOKEN is not defined!' );
  68. }
  69.          
  70. $signature = $_GET [ "signature" ];
  71. $timestamp = $_GET [ "timestamp" ];
  72. $nonce = $_GET [ "nonce" ];
  73.                  
  74. $token = TOKEN;
  75. $tmpArr = array ( $token , $timestamp , $nonce );
  76. // use SORT_STRING rule  
  77. sort( $tmpArr , SORT_STRING);
  78. $tmpStr = implode( $tmpArr );
  79. $tmpStr = sha1( $tmpStr );
  80.          
  81. if ( $tmpStr == $signature ) {
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. }
  88.  
  89. ?>

Step 3: Implement business logic based on interface documentation

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

From then on, every time a user sends a message to the official account or generates a custom menu click event, the server configuration URL filled in by the developer will receive the messages and events pushed by the WeChat server, and then the developer can respond according to their own business logic, such as replying to messages.

When the official account calls each interface, it will generally get the correct result. For specific results, see the description of the corresponding interface. When an error is returned, you can query the cause of the error based on the return code. Global return code description

When a user sends a message to a public account, the public account receives an OpenID from the sender of the message, which is the result of encryption using the user's WeChat account. Each user has a unique OpenID for each public account.

In addition, since developers often need to share user accounts and unify the account system between multiple platforms (mobile applications, websites, public accounts), WeChat Open Platform (open.weixin.qq.com) provides a UnionID mechanism. Developers can obtain basic user information through OpenID. If developers have multiple applications (mobile applications, website applications, and public accounts, public accounts will only obtain UnionID after being bound to WeChat Open Platform accounts), they can distinguish the uniqueness of users by obtaining UnionID in the user's basic information, because as long as they 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 see the WeChat Open Platform Resource Center-Mobile Application Development-WeChat Login-Authorization Relationship Interface Call Guide-Obtaining User Personal Information (UnionID Mechanism).

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

<<:  Tencent X5 and Egret Runtime jointly promote HTML5 game development

>>:  WeChat Developer Guidelines

Recommend

How to start promotion from scratch?

From Internet to Mobile Internet At a time when s...

When using your mobile phone, do you prefer "normal mode" or "dark mode"?

I still remember when the dark mode was first upd...

The three stages and effects of the massive Qianchuan launch!

Since its full launch, Juliang Qianchuan has been...

Interesting Talk | If AI has autonomous consciousness, where will humanity go?

The topic of AI has been discussed for many years...

The big screen is not the goal but a means. Where is the smart car heading?

The automotive industry, which has a history of m...

The quality of an app does not lie in its promotion, but in its

The quality of an APP does not lie in its promoti...

Five of the best apps to control your PC from your Android smartphone

【51CTO.com Quick Translation】Think about those la...

Internet Activities: Telling a Very Obscene Story

When you see this line of words, you have already...