< span="">
The public platform provides a simple PHP sample code that we need to learn more about before further development.
< span="">
Official website: http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.zip
< span="">
The complete code is as follows:
("TOKEN", "Weixin"=- =["Echostr" (- =["Http_raw_post_data" (!(=(, ' SimpleXMLElement ',=-=-=(-== "</strong></strong>
%s
%s
%s
0 %s
%s
"(!(= "Text"= "Welcome to WeChat world!"=(,,,,, "Input something ...""" =["Signature"=["Timestamp"=["Nonce"==(,,(=(=((== ?>3.1 Overall analysis
The original sample code is roughly divided into four parts:
- Defining tokens
- Declare a class of wechatcallbackapitest
- Creates an instance object of class Wechatcallbackapitest $WECHATOBJ
- invokes the valid () method of the class.
3.2 Detailed analysis
3.2.1 Defining tokens
< p="">
< p="">
< p="">
3.2.2 Declaration of a class
< p="">
< p="">
< p="">
< p="">
< p="">
< p="">
< p="">
the responsemsg function is detailed:
$postStr = $GLOBALS ["Http_raw_post_data"];
Receives the user message sent by the public platform, the message data structure is XML, is not the PHP default recognition data type, therefore uses the $globals[' Http_raw_post_data '] to receive, simultaneously assigns the value to the $POSTSTR
if (!empty ($POSTSTR))
Determines whether the $poststr is empty, if it is not empty (data is received), proceeds to execute the following statement, or, if empty, jumps to the else statement corresponding to it.
$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. This rigorous writing also has to add a judgment on whether to load a successful conditional statement, but do not write is OK.
$fromUsername = $POSTOBJ->fromusername;
Assigns the user's OpenID in the object $postobj to the $fromusername variable
$toUsername = $POSTOBJ->tousername;
Assigns 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 a string, where the user-entered keyword is available
$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 GMT, January 1, 1970) to the current time.
$TEXTTPL = "
%s
%s
%s
%s
%s
0
";
Template for storing output content
if (!empty ($keyword))
Determines whether the $keyword is empty, continues execution of the following statement, or, if NULL, jumps to the else statement corresponding to it, that is, echo "Input something ...";
$msgType = "text";
Message type is text type
$CONTENTSTR = "Welcome to WeChat world!";
Message content of reply
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Use the sprintf () function to write formatted data to a variable;
$fromUsername, $toUsername, $time, $msgType, $contentStr the "%s" position in the template is replaced sequentially, or the variable "$RESULTSTR" is finally actually:
$toUsername
$fromUsername
$time
$msgType
$contentStr
0
//
bit 0x0001 is marked when the star has just received the message.
Echo $resultStr; Output the message of the reply
< p="">
< span="">
< p="">
< span="">
< span="">
< span="">
3.2.4 Calling class method validation
$WECHATOBJ->valid ();
< p="">
< span="">
The above is an analysis of the official sample code, there is no explanation of the place, also ask the master point. In addition, the code is simply an official example of the code, if you want to do complex development, or require developers to follow the rigorous development model to rewrite the code, will be in the next tutorial to explain.
< span="">
Official Public Platform API documentation: http://mp.weixin.qq.com/wiki/index.php
< span="">
Please pay attention to Zhuo Kam Suzhou public account, Zhuo Kam Suzhou based on the development of SAE platform, for the mainstream function of the development test.
You can take a look at the Suzhou public account to perform functional testing and get new application development.
1. Login client, friends, add friends--Search number---Zhuojinsz, find and follow.
2. Scan QR Code:
Zhuo Jin Suzhou feature List.
< strong="">
< p="">
< p="">
< li="">
< li="">
< li="">
< strong="">We believe, great people Share knowledge ...
http://www.bkjia.com/PHPjc/440310.html www.bkjia.com true http://www.bkjia.com/PHPjc/440310.html techarticle The public platform provides a simple PHP sample code that we need to learn more about before further development. Official website: http://mp.weixin.qq.com/mpres/ht ...