WeChat public platform development (2) WeChat public platform sample code analysis-PHP Tutorial

Source: Internet
Author: User
Tags php sample code
Public platform development (2) public platform sample code analysis. The public platform provides a simple php sample code. before further development, we need to take a closer look at it. Official website: mp. weixin. qq. comatrix Resht

The public platform provides a simple php sample code. before further development, we need to take a closer look at it.

Official website: http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.zip

The complete code is as follows:

   = ["echostr"        (->           = ["HTTP_RAW_POST_DATA"         (!( = (, 'SimpleXMLElement', = -> = -> = (-> =  = "                            
 %s
                             
 %s
                             
 
  %s
                             
 %s
                             
 %s
                             
 
  0
                             "(!(  = "text" = "Welcome to wechat world!" = (, , , , ,   "Input something..." ""  = ["signature" = ["timestamp" = ["nonce" = = (, , ( = (  = ( (  ==   ?>

3.1 Overall analysis

The original sample code is roughly divided into four parts:

  • Define TOKEN
  • Declare a wechatCallbackapiTest class
  • Create an instance object like wechatCallbackapiTest $ wechatObj
  • Call the valid () method of the class.

3.2 Detailed analysis

3.2.1 define TOKEN

3.2.2 declare a class

ResponseMsg Function details:

$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
Receive the user message sent from the public platform. the Message data structure is XML, which is not the default data type recognized by php. Therefore, $ GLOBALS ['http _ RAW_POST_DATA '] is used to receive the message, $ postStr

If (! Empty ($ postStr ))
Determine whether $ postStr is null. if it is not null (data is received), continue to execute the following statement. if it is null, 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 to the $ postObj object. This rigorous writing method is followed by a conditional statement to determine whether the load is successful, but it is okay if you do not write it.

$ FromUsername = $ postObj-> FromUserName;
Assign the OPENID of the sender of 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 spaces and other pre-defined characters from both ends of the string. here, 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 from the Unix epochs (00:00:00 GMT, January 1, January 1, 1970) to the current time.

$ TextTpl ="
        %s
        %s
       % S
        %s
        %s
        0
        ";
Template for storing Output Content

If (! Empty ($ keyword ))
Judge whether $ keyword is null. if it is not null, continue to execute the following statement. if it is null, jump to the corresponding else statement, that is, echo "Input something... ";

$ MsgType = "text ";
The message type is text.

$ ContentStr = "Welcome to wechat world! ";
Reply message content

$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Use the sprintf () function to Write formatted data to the variable;
$ FromUsername, $ toUsername, $ time, $ msgType, $ contentStr, respectively, sequentially replace the "% s" position in the template, that is, the variable "$ resultStr" is actually:


$toUsername
$fromUsername
$ Time
$msgType
$contentStr
0 // The message received by the star when 0x0001 is marked.

Echo $ resultStr; // output the reply message

 

3.2.4 Call method verification

Valid ();

The above is an analysis of the official sample code. if the explanation is incorrect, please point it out. In addition, this code is just a simple sample code officially provided. if you want to perform complex development, you must rewrite the code in a rigorous development mode. we will explain it in subsequent tutorials.

Official public platform API documentation: http://mp.weixin.qq.com/wiki/index.php

Please follow Zhuo Jin SuzhouPublic account, Zhuo Jin SuzhouDeveloped and tested based on the SAE platform for mainstream features.

You can follow Zhuo Jin SuzhouThe public account performs function tests and obtains new application development.

1. log on to the client, and add a friend to search for the zhuojinsz number.

2. scan the QR code:

Zhuo Jin SuzhouFunction list.

    We Believe, Great People Share Knowledge...

    Bytes. Official website: http://mp.weixin.qq.com/mpres/ht...

    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.