[Play to WeChat public platform 10] Text message reply explanation

Source: Internet
Author: User
Tags openid

There are many types of messages that we reply to users. They can be simple text replies, music replies, or text replies. (You must not reply to only one image, but must include text ). Why do I only reply via text in this article? There are multiple reasons:
1. I have not made any music reply;
2. Reply to the previous article;
3. I personally think it is a little difficult to reply via text and text, so I will focus on it;
4. the blogger has to get together the blog articles... (yes, correct)


--------------------------
1. Before explaining the text message reply, I want to add something: In the blog that explains the sample code, the following code jumps over:

$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";

$ TextTplHow does this product look so familiar? Yes, we are replying to the user's text message ( Note: Text Messages), As shown in the following sentence:
$ MsgType = "text"; $ contentStr = "Thank you for your attention. Reply" 1 "to view the blogger ID; reply" 2 "to view the blogger hobbies "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr;

It is used to synthesize text messages that we want to send to users into xml text. This text contains the user's OpenID, public ID, system time, and message type, the text of the reply.So what is xml format?

XML is a simple data storage language. It requires that each tag has a start and end tag and can be nested. Each field in it (that is, each type of information) must start with <>, </>, As follows:
<Xml> <content> <id> blogger ID </id> <age> age </age> </content> </xml>
Xml simple analysis here, Let's look back at the $ textTpl variable.:
$ TextTpl = "<xml> // The format is xml <ToUserName> <! [CDATA [% s]> </ToUserName> // ID of the Public Account <FromUserName> <! [CDATA [% s]> </FromUserName> // storage user OpenID <CreateTime> % s </CreateTime> // storage system time <MsgType> <! [CDATA [% s]> </MsgType> // stores the Message Type <Content> <! [CDATA [% s]> </Content> // store the Content to be sent to the Message <FuncFlag> 0 </FuncFlag> // determines whether the star is Star (leave this alone) </xml> ";

Well, the above Code comments are quite understandable, The only thing that may not be understood is [CDATA [% S]. CDATA indicates that it is not escaped. If you don't understand it, it will be okay. Let's look at % s, it indicates the data type to be converted to a string. % S indicates a newborn baby. The string type of this baby (which can be understood as a female). We all know that all information is blank at birth, no name, no account, and so on. This is the meaning of % s, indicating a blank space, waiting for you to develop,See the following sentence:
<Content> <! [CDATA [% s]> </Content>
At this time, there is no Content in the Content, so we need to transfer the Content into. How can we transfer it? Use Sprintf ()The format of this function is as follows:
Sprintf (baby, $ name, $ country ,...);
The first parameter "baby" indicates the xml format variable (baby) of the content to be transmitted, and $ name is sent to the first location in the baby that appears % s, pass $ country to 2nd locations where % s appears, and so on
.

Finally, let's look back at this sentence:
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);echo $resultStr;
Do you know what's going on? $ TextTpl is an xml format variable. Next, $ fromUsername is passed to textTpl, then tuUsername, And then.... Finally, the new textTpl is passed to the resultStr variable and then sent to the user.

2. Next, let's go to the question: how to reply to text messages.
Below are some formats for replying text messages:


You can also click this link to View Details: Click to open the link

The description above is very detailed, Descripition is displayed in a single graph, but not in multiple graphs. The first one is an item big image with a pixel of 640x320. The following item image is a small image, the pixel is 80x80, and the Url is the link of the image, so before that, we need to upload the image to the SAE server (or where else will your image link come from ?).
Next, I will first create a single image reply mode. First, configure the xml of the text message, similar to the $ textTpl of the text message.
$ ImageTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> // The message type is news (text) <ArticleCount> 1 </ArticleCount> // The number of texts is 1 (single text) <Articles> <item> // The first text message <Title> <! [CDATA [% s]> </Title> // Title <Description> <! [CDATA []> </Description> // The Description is empty (too lazy to describe) <PicUrl> <! [CDATA [% s]> </PicUrl> // the Url of the previous image <Url> <! [CDATA [% s]> </Url> // click the link address of the image to be displayed. </item> </Articles> </xml> ";

Now, the reply template for text and text messages has been completed. The next step is to fill up the title and image information of the image message. The Code is as follows:
$ Title = "I go, it's raining again"; // title $ PicUrl = "http://1.star530.sinaapp.com/weather.jpg"; // picture link $ Url = "http://1.star530.sinaapp.com/weather.jpg "; // link to the opened image $ resultStr = sprintf ($ imageTpl, $ fromUsername, $ toUsername, $ time, $ title, $ PicUrl, $ Url); echo $ resultStr;
The running effect is as follows:


Well, the complete code is as follows:
Public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post dataif (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> 0 </FuncFlag> </xml> "; $ imageTpl =" <xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> // The message type is news (text) <ArticleCount> 1 </ArticleCount> // The number of texts is 1 (single text) <Articles> <item> // The first text message <Title> <! [CDATA [% s]> </Title> // Title <Description> <! [CDATA []> </Description> // The Description is empty (too lazy to describe) <PicUrl> <! [CDATA [% s]> </PicUrl> // the Url of the previous image <Url> <! [CDATA [% s]> </Url> // click the link address of the image to be displayed </item> </Articles> </xml> "; if (! Empty ($ keyword) {$ title = "I go, it's raining again"; // title $ PicUrl = "http://1.star530.sinaapp.com/weather.jpg"; // picture link $ Url = "http://1.star530.sinaapp.com/weather.jpg "; // link to the opened image $ resultStr = sprintf ($ imageTpl, $ fromUsername, $ toUsername, $ time, $ title, $ PicUrl, $ Url); echo $ resultStr ;} else {echo "Input something... ";}} else {echo" "; exit ;}}}

Okay, that's it.

It was originally scheduled to finish this series of tutorials in one week. As a result, only 10 tutorials were completed for various reasons. So, let's write it here first, and wait for the next time to take the time to continue...

Coming soon ~~

Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/26139129

Related Article

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.