This article introduces the content is about the public development of a complete tutorial two, has a certain reference value, now share to everyone, the need for friends can refer to
because of the needs of the work, this two years of public numbers and small procedures, project production more. That's why I'm going to write a full-fledged tutorial, of course, the best tutorial is the documentation for the work platform. I'm just here to tell you about the production process in my work. All relevant articles of the source, I hosted on my own GitHub above, welcome to the attention: address click Open Link . Next, we'll start our tutorial.
In the first section I described the opening and simple acquisition of developer mode Access_token and caching, as well as simple tests, this section we don't need to use this for the time being
Access_token parameters, most of the first to describe the part of our test code, and improve it: (I will introduce Baidu map and Turing robot technology), because the company developed the use of such technology, so I would like to tell you: start:
1. Acceptance of the message:
when a message is accepted, the message is divided according to the user's content. There are text messages, picture messages, language messages, videos, connections and other messages. A custom URL address is requested each time the user interacts with the public platform to generate a message . In the process of the request, various parameters are transmitted using XML format. Receiving the message takes the relevant data from the process of the request.
Variable substitution in the code, using PHP functions
sprintf
As you can see, it's just that the variables can be replaced in turn.
1.1. Accept text messages:
Format as above: Code as follows: text template
Text templates
$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> ";
The results of the test everyone in the first section has been seen, and I am not in the demo again
1.2 Accept Picture message:
The code is as follows:
Picture templates
$PICTPL = "<xml> <tousername><![ Cdata[%s]]></tousername> <fromusername><![ cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><![ cdata[%s]]></msgtype> <Image> <mediaid><![ cdata[%s]]></mediaid> </Image> </xml> ";
The code is as follows:
The test results are:
1.3: Accept the language message:
The template is as follows:
The code is as follows:
}elseif ($msgType = = "Voice") {$CONTENTSTR = "voice message mediaid:". $POSTOBJ->mediaid. ' Specific content: '. $POSTOBJ->recognition; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr); echo $resultStr;}
The results are as follows: We used one of the above code:
Attach a picture of my test results:
1.4 Receive Video message:
The documentation is as follows:
The code is as follows:
ElseIf ($msgType = = "Video") {$contentStr = "MediaID:". $POSTOBJ->mediaid; $resultStr = sprintf ($TEXTTPL, $ Fromusername, $toUsername, $time, ' text ', $contentStr); echo $resultStr;}
1.5: Accept the location:
Code:
ElseIf ($MsgType = = ' Location ') { $contentStr = "Longitude:". $POSTOBJ->location_y. ' Dimension '. $POSTOBJ->location_ X. ' Specific address: '. $POSTOBJ->label; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr); File_put_contents (' 2 ', $resultStr); echo $resultStr; }
The results are as follows:
1.6: Accept the link message:
The documentation is as follows:
The code is as follows:
ElseIf ($MsgType = = ' link ') { $contentStr = ' message is titled '. $POSTOBJ->title; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr); File_put_contents (' 2 ', $resultStr); echo $resultStr; }
The results are as follows:
The above cases are messages that are accepted using the text template. Sets the content of the reply. Next we enrich the format of the reply content:
2. Reply to the message:
2.1 Replies. Text Message:
The format is as follows:
The code is as follows:
2.2: Reply to Picture message:
The code is as follows:
The results are as follows:
2.3 Replies to voice messages:
Template:
$VOICETPL = "<xml> <tousername><![ Cdata[%s]]></tousername> <fromusername><![ cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><![ cdata[%s]]></msgtype> <Voice> <mediaid><![ cdata[%s]]></mediaid> </Voice> </xml> ";
Reply code:
ElseIf ($keyword = = ' Voice ') { //about this mediaid need to be obtained from the library, no media ID can be returned temporarily using the message $MediaId = ' 3xlxz4-r2otnytfakcmpwv4qjwtwg_15b4pytqjvwoawhpofc38mgztsdkdxx9po '; $RESULTSTR = sprintf ($VOICETPL, $fromUsername, $toUsername, $time, ' Voice ', $MediaId); echo $resultStr; }
The results are as follows:
2.4: Reply to video message:
The template is as follows:
Online Debug Interface upload video footage:
Video templates:
$VIDEOTPL = "<xml> <tousername><![ Cdata[%s]]></tousername> <fromusername><![ cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><![ cdata[%s]]></msgtype> <Video> <mediaid><![ Cdata[%s]]></mediaid> <title><![ Cdata[%s]]></title> <description><![ cdata[%s]]></description> </Video> </xml> ";
The reply code is as follows:
ElseIf ($keyword = = "Video") {//about this mediaid need to be obtained from the library, no media id$mediaid= can be returned using a temporary message " Xxmyaopbut1u3q5z95xrhafnzyvl3tg08e-9ub2m6db_elj4xajhr2puoqlhreyb "; $Title = $Description =" video or good-looking "; $resultStr = sprintf ($VIDEOTPL, $fromUsername, $toUsername, $time, ' video ', $MediaId, $Title, $Description); echo $resultStr;}
The results are as follows:
2.5: Reply text message:
Document Information:
Graphic Template:
Graphic templates
$newsTpc = "<xml> <tousername><![ Cdata[%s]]></tousername> <fromusername><![ cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><![ cdata[%s]]></msgtype> <ArticleCount>%d</ArticleCount> <articles>%s</ articles> </xml> ";
Reply code:
elseif ($keyword = = "Graphics") {$data = array (' Title ' = ' + ' text message ', ' Description ' = ' effect seems good ah ', ' picurl ' = ' http://mmbiz.qpic.cn/mmbiz_jpg/ e3tene8jstaqus3ic5qett4wl14ibbu4uaobarztvop18awt83hkzm0ai9xstapn4xay6ji4lfm0h7qnksfxqyva/0 ', ' Url ' =/http Xiaomi.com ') file_put_contents (' 2 ', $data [0][' Title ']); for ($i =0; $i <count ($data); $i + +) {$Articles. = "<item > <title><! [cdata[{$data [$i] [' Title ']}]]></title> <description><! [cdata[{$data [$i] [' Description ']}]]></description> <picurl><! [cdata[{$data [$i] [' Picurl ']}]]></picurl> <url><! [cdata[{$data [$i] [' Url ']}]]></url> </item> ';} $count = count ($data); $resultStr = sprintf ($newsTpc, $fromUsername, $toUsername, $time, ' News ', $count, $Articles); echo $ ResultStr;}
The results are as follows:
So far we have written and demonstrated all the codes of acceptance and response, all the source code I'll put on my github, you can download and focus, this section ends here, and the next section starts using our Access_token to start our custom menu.