Basic principles of WeChat public platform development and messaging

Source: Internet
Author: User
Tags sha1 encryption
The server is equivalent to a forwarding server. a terminal (such as a mobile phone or tablet) initiates a request to the server and forwards the request to the custom service (this is our specific implementation ). I. Basic principles

Before you start, you may be very interested in this, but you are quite confused. Is it complicated? Difficult to learn?

In fact, it is quite simple. To eliminate your concerns, I first briefly introduced the basic principles of the public platform.

The server is equivalent to a forwarding server. a terminal (such as a mobile phone or tablet) initiates a request to the server and forwards the request to the custom service (this is our specific implementation ).

After the service is processed, it is then volatile to the server, and then the server returns the specific response to the terminal.

The communication protocol is HTTP.

The data format is XML.

The specific process is shown in:

What we need to do is to respond to HTTP requests.

The specific request content is parsed according to the specific XML format. after processing, it must be returned according to the specific XML format.

We only need a simple implementation of HttpHandler.

Of course, the platform can also implement more complex services. for example, it can be used as an embedded browser. we can open the htm interface through a link and then implement our own logic.

II. Message Interface (official documentation)Message application interface

Click Apply and enter the url and token. you can enter the token as needed to generate a signature.

Website Access

After a public platform user submits information, the server sends a GET request to the URL filled in with four parameters:

Parameters Description
Signature Encrypted signature
Timestamp Timestamp
Nonce Random number
Echostr Random string

The developer verifies the request by verifying signature (The following is a verification method ). If you confirm that the GET request is from the server, the echostr parameter is returned as is, and the access takes effect. Otherwise, the access fails.

Signature combines the token parameter entered by the developer with the timestamp parameter and nonce parameter in the request.

Encryption/verification process: 1. sort the tokens, timestamp, and nonce in lexicographic order. splice the three parameter strings into one string for sha1 encryption. the encrypted string obtained by the developer can be compared with signature to identify that the request comes from

Message push

When a common user sends a message to a public account, the server will POST the message to the entered URL. The structure is as follows:

Text message
 
  
  toUser
   
  fromUser
    
  
   1348831860
   
  text
   
  this is a test
   
  
   1234567890123456
   
 
Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Text
Content Text message content
MsgId Message id, 64-bit integer
Image message
 
  
  toUser
   
  fromUser
   
  
   1348831860
   
  image
   
  this is a url
   
  
   1234567890123456
   
 
Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Image
PicUrl Image link
MsgId Message id, 64-bit integer
Geographic location message
 
  toUser
  
  fromUser
  
  
   
1351776360
  
  location
  
  
   
23.134521
  
  
   
113.358803
  
  
   
20
  
  位置信息
  
   
1234567890123456
  
 
Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Location
Location_X Geographic location and latitude
Location_Y Geographic longitude
Scale Map zoom
Label Geographic location information
MsgId Message id, 64-bit integer
Link message
 
  toUser
  
  fromUser
  
  
   
1351776360
  
  link
  
  <! [CDATA [Official website of the public platform]>
  公众平台官网链接
  
  url
  
  
   
1234567890123456
  
 
Parameters Description
ToUserName Recipient number
FromUserName Sender ID. if it is a common user, it is an OpenID.
CreateTime Message creation time
MsgType Message type, link
Title Message title
Description Message description
Url Message link
MsgId Message id, 64-bit integer
Event push

Event push only supports version 4.5. Currently, you can enable event push, follow, and cancel follow event push for custom menu interfaces. Other functions will be available soon.

 
  toUser
  
  FromUser
  
  
   123456789
  
  event
  
  EVENT
  
  EVENTKEY
  
 
Parameters Description
ToUserName Recipient number
FromUserName Sender ID. if it is a common user, it is an OpenID.
CreateTime Message creation time
MsgType Message type, event
Event Event Type: subscribe, unsubscribe, and CLICK)
EventKey Event KEY value, which corresponds to the KEY value in the custom menu interface
Message Reply

For each POST request, the developer returns a specific xml structure in the response packet to respond to the message (now supports replying to text, text, voice, video,Music).

The serverWithin five secondsIf no response is received, the connection will be disconnected.

The xml structure of the reply is as follows:

Reply text message
 
  
  toUser
   
  fromUser
   
  
   12345678
   
  text
   
  content
   
 
Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message creation time
MsgType Text
Content The content of the reply message. the length cannot exceed 2048 bytes.
Reply music message
 
  
  toUser
   
  fromUser
   
  
   12345678
   
  music
   
   
   <![CDATA[TITLE]]> 
   DESCRIPTION
    
   MUSIC_Url
    
   HQ_MUSIC_Url
    
   
 
Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message creation time
MsgType Music
MusicUrl Music link
HQMusicUrl High-quality music link, which is preferred for playing music in WIFI environments
Reply to text message
 
  
  toUser
   
  fromUser
   
  
   12345678
   
  news
   2  
   
   <![CDATA[title1]]>  
   description1
    
   picurl
    
   url
    
   
   
   <![CDATA[title]]> 
   description
    
   picurl
    
   url
    
    
 
Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message creation time
MsgType News
ArticleCount Number of text and text messages, up to 10
Articles Multiple text messages. The first item is a large image by default.
Title Text message title
Description Text message description
PicUrl Image links. JPG and PNG formats are supported. The effect is 40*320, and the image size is 80*80.
Url Click the text message jump link
Official interface documentation:

Http://mp.weixin.qq.com/wiki/index.php? Title = % E6 % B6 % 88% E6 % 81% AF % E6 % 8E % A5 % E5 % 8F % A3 % E6 % 8C % 87% E5 % 8D % 97

Notes

1. the user's OpenID is a fixed and unique string for a public number.

2. use port 80

Please note: We will explain the specific development process in the future. 3. message class diagram

For more articles about the basic principles and messaging of public platform development, please refer to the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.