Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of the Code of php Wechat Public platform

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the sample analysis of the php Wechat public platform code. The content of the article is of high quality, so the editor shares it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The Wechat public platform provides a simple php sample code, which we need to know in detail before further development.

Get the code

Wechat official website: http://xiazai.jb51.net/201612/yuanma/phpwxsample(jb51.net).rar

Analysis code

The complete code is as follows:

1.1 overall analysis

The original sample code is roughly divided into four parts:

Define TOKEN

Declare a class wechatCallbackapiTest

Create an instance object of class wechatCallbackapiTest $wechatObj

Call the valid () method of the class.

1.2 detailed analysis

3.2.1 define TOKEN

Define ("TOKEN", "weixin")

Define is a function used to assign values to constants, which means to give the constant value "TOKEN" the value "weixin".

TOKEN is used for interaction security authentication, and developers can define it as they like on the public platform.

3.2.2 declare a class

Class wechatCallbackapiTest {

}

Declare a class wechatCallbackapiTest that contains three methods (functions).

A. Public function valid ()

Used to send verification information to Wechat when applying to become a developer.

B. public function responseMsg ()

Processing and replying to messages sent by users is also the most frequently used function, and almost all the functions are implemented here.

Detailed explanation of responseMsg function:

$postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]

Receive the user message sent by Wechat public platform. The data structure of the message is XML, which is not the default identification data type of php, so here $GLOBALS ['HTTP_RAW_POST_DATA'] is used to receive it, and a value is assigned to $postStr.

If (! empty ($postStr))

Determine if $postStr is empty, and if not (data is received), continue with the following statement; if so, jump to the corresponding else statement.

$postObj = simplexml_load_string ($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)

Use the simplexml_load_string () function to load the received XML message data into the object $postObj. After this rigorous writing, you have to add a conditional statement to determine whether it is loaded successfully, but it's okay if you don't write it.

$fromUsername = $postObj- > FromUserName

Assign the OPENID of the sending message user in the object $postObj to the $fromUsername variable

$toUsername = $postObj- > ToUserName

Assign the ID of the public account in the object $postObj to the $toUsername variable

$keyword = trim ($postObj- > Content)

The trim () function removes white space characters and other predefined characters from both ends of the string, and you can get the keywords entered by the user.

$time = time ()

The time () function returns the Unix timestamp of the current time, that is, the number of seconds since the Unix era (00:00:00 on January 1, 1970 GMT) to the current time.

$textTpl = "% s 0"

A template for storing the output of Wechat

If (! empty ($keyword))

Determine whether $keyword is empty, and if not, continue to execute the following statement; if empty, jump to the corresponding else statement, that is, echo "Input something..."

$msgType = "text"

The message type is a text type

$contentStr = "Welcome to wechat world!"

The content of the reply message

$resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr)

Use the sprintf () function to write formatted data to variables

$fromUsername, $toUsername, $time, $msgType, and $contentStr replace the "% s" position in the template in sequence, that is, the variable "$resultStr" is actually:

The star just received the message when the $time0 / bit 0x0001 was marked.

Echo $resultStr; / / output the reply message

C. Private function checkSignature ()

The developer verifies the request by verifying the signature (there is a verification method below). If it is confirmed that the GET request is from Wechat server and the echostr parameter content is returned as is, the access will take effect, otherwise the access will fail.

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

Encryption / verification process:

1. The three parameters token, timestamp and nonce are sorted in dictionary order.

two。 Concatenate three parameter strings into one string for sha1 encryption

3. The encrypted string obtained by the developer can be compared with signature, indicating that the request originated from Wechat

3.2.3 create an instance object

$wechatObj = new wechatCallbackapiTest ()

3.2.4 calling class method verification

$wechatObj- > valid ()

Call the valid () method of the class to perform interface verification, and comment it out when the interface is set successfully.

The above is an analysis of the official sample code of Wechat. If there is any incorrect explanation, please point it out.

This is the end of the sample analysis of the php Wechat public platform code. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report