Php WeChat public platform interaction and interface details, php public platform details

Source: Internet
Author: User

Php public platform interaction and interface explanation, php public platform explanation

This article is divided into three parts for your introduction. The specific content is as follows:

1. Interaction between users, servers, and backend servers

For example, if a user sends a text message to the public account, the message is first sent to the server. The server processes the message and transmits it to the backend server in xml format, after the backend server receives the data, it processes the data and then delivers the response data to the server in xml format. The server then responds to the user interface.
The interaction process between the user and the backend server is the data transfer process, but it only needs to pass through the server.

So what is the purpose of the server transfer station?
After the xml data is processed and packaged, it is displayed on the mobile phone screen. The text message we accept is as follows:

Single Text:

Multi-Image

You will find that almost all images and text are in this format, and the plates and sizes are the same. This is the result of server packaging.

2. Interactive Data Types

Data Types that users can send
1. text)

<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";

2. voice (voice)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [voice]> </MsgType> <Format> <! [CDATA [amr]> </Format> <MsgId> 5836982871638042400 </MsgId> <MediaId> <! [CDATA [pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvl]> </MediaId> <Recognition> <! [CDATA []> </Recognition> // recognition indicates the speech Recognition result. </xml>

3. Images (img)

<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/L4qjYtOibummHn90t1mnaibYiaR8ljyicF3MW7XX3BLp1qZgUb7CtZ0D]]></PicUrl><MsgId>5836982871638042400</MsgId><MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId></xml>

After each message is sent to the server, a MsgId is marked, and the uploaded image, video, and voice are also marked with a mediaId.

4. video)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [vedio]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <MediaId> <! [CDATA [pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvl]> </MediaId> <ThumbMediaId> <! [CDATA [screenshot]> </ThumbMediald>; // the address of the image displayed when the video is static </xml>

5. location)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [location]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <Location_X> 22.539968 </Location_X> <Location_Y> 113.954980 </Location_Y> <Scale> 16 </Scale> <Label> <! [CDATA [ZIP code: 9001, No. 518053 ShenNan Avenue, Nanshan District, Shenzhen City, Guangdong Province, China]> </Label> </xml>

6. link)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [link]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <Title> <! [CDATA [public platform developers]> </Title> <Description> <! [CDATA [Chen Kun's public account, this time, everyone...]> </Description> <Url> <! [CDATA [http://www.cnblogs.com/txw1958/133]> </Url> <MsgId> 5839907284805129867 </MsgId> </xml>

Type of the message returned by the backend server
1. text)
2. voice (voice)

<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[voice]]></MsgType><MsgId>5836982871638042400</MsgId><Voice><MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId></Voice></xml>

3. Images (img)

<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><MsgId>5836982871638042400</MsgId><Image><MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId></Image></xml>

4. video)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [vedio]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <video> <MediaId> <! [CDATA [pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvl]> </MediaId> <ThumbMediaId> <! [CDATA [screenshot]> </ThumbMediald>; // the address of the image displayed when the video is static </video> </xml>

5. music)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [voice]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <Music> <Title> <! [CDATA [the best national style]> </Title> <Description> <! [CDATA [phoenix legend]> </Description> <MusicUrl> <! [CDATA [http://zj189.cn/zj/download/music/zxmzf.mp3#]> </MusicUrl> <HQMusicUrl> <! [CDATA [http://zj189.cn/zj/dodownload/music/zxmzf.mp3#]> </HQMusicUrl> </Music> </xml>

6. news)

<Xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> <MsgId> 5836982871638042400 </MsgId> <Content> <! [CDATA []> </Content> <ArticleCount> % s </ArticleCount> <Articles> <item> <Title> <! [CDATA [[Shenzhen] Live temperature: 6 ℃ humidity: 62 wind speed: northeast wind Level 2]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://www.doucube.com/weixin/weather/icon/banner.jpg?]> </PicUrl> <Url> <! [CDATA []> </Url> </item> <Title> <! [CDATA [[Shenzhen] Live temperature: 6 ℃ humidity: 62 wind speed: northeast wind Level 2]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://www.doucube.com/weixin/weather/icon/banner.jpg?]> </PicUrl> <Url> <! [CDATA []> </Url> </item> </Articles> </xml>

The above Code only serves as a reference for data filling. The preceding code can be called as needed. The following data format is displayed here.
CDATA is a tag. The text data marked by CDATA is not parsed by the xml parser. A CDATA part uses"

ToUserName receiver account
FromUserName sender account
CreateTime send event
MsgType Data Type
Content text Content
ArticleCount
MsgId data id
MediaId media id
Title
Description
MusicUrl music connection address
HQMusicUrl high-quality music connection address

2. Specific interaction steps are code

In Figure 2 of the previous chapter, we defined the url and token for the test number. A url is the backend server address that communicates with the server, and a token is equivalent to a token. This token is displayed when the Server communicates with the background server. If the background server finds that the server is the same as the token it carries, the communication is rejected. This process is called token verification (this token is not the value of the token ).
The above is an image. I will explain it through code below.
For example, url is http://weixinceshi111111.applinzi.com/index2.php
Token: weixin
Index2.php code

<? Php //// response to user messages // different message types that the public account responds to the user // The server must communicate with the backend server before performing token verification, signature (encrypted signature), nonce (random number), timestamp (timestamp), and echostr (random string) are sent through get ). After the backend server obtains the TOKEN, it concatenates timestamp and nonce into strings in a certain order and compares the obtained result with signature after shal encryption, if they are the same, echostr is returned to the server. Indicates that the verification is successful. Header ("content-type: text; charset = utf8"); define ("TOKEN", "weixin"); // token indicates that the data is transmitted through get, user-sent data is sent by post. First get request and then post request. $ WechatObj = new wechatCallbackapiTest (); // determines whether the request is a get request or a post request. $ _ GET ['echostr'] If it exists, it indicates a get request for token verification. The opposite is the post request for data transmission. If (! Isset ($ _ GET ['echostr']) {$ wechatObj-> responseMsg (); // response data} else {$ wechatObj-> valid (); // response} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr; exit ;}} private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp =$ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ time Stamp, $ nonce); sort ($ tmpArr); // sort the elements in the array $ tmpStr = implode ($ tmpArr ); // Concatenates the elements in the array into a string $ tmpStr = sha1 ($ tmpStr); // encrypts the string. If ($ tmpStr ==$ signature) {return true;} else {return false ;}} public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // obtain the sent data. If (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement ',); // load the xml string into a SimpleXMLElement object. Simplexml_load_string () is an xml parser. $ RX_TYPE = trim ($ postObj-> MsgType); // trim removes kongge from both ends of the string. // Switch ($ RX_TYPE) {case "text": $ result = $ this-> receiveText ($ postObj); break; case "image ": $ result = $ this-> receiveImage ($ postObj); break; case "voice": $ result = $ this-> incluevoice ($ postObj); break; case "video ": $ result = $ this-> receiveVideo ($ postObj); break; default: $ result = "unknow msg type :". $ RX_TYPE; break;} echo $ result;} else {echo "; exit;} private function rece IveText ($ object) {$ keyword = trim ($ object-> Content); if ($ keyword = "text ") {// reply to a text message $ content = "this is a text message"; $ result = $ this-> transmitText ($ object, $ content );} else if ($ keyword = "" | $ keyword = "") {// reply to a single message $ content = array (); $ content [] = array ("Title" => "Single Text Title", "Description" => "Single Text content", "PicUrl" => "http://discuz.comli.com/weixin/weather/icon/cartoon.jpg ", "Url" => "http://m.cnblogs.com /? U = txw1958 "); $ result = $ this-> transmitNews ($ object, $ content);} else if ($ keyword =" Multi-image ") {// reply to multi-text message $ content = array (); $ content [] = array ("Title" => "Multi-text 1 Title ", "Description" => "", "PicUrl" => "http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => "http://m.cnblogs.com /? U = txw1958 "); $ content [] = array (" Title "=>" Multi-Image 2 Title "," Description "=> "", "PicUrl" => "http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" => "http://m.cnblogs.com /? U = txw1958 "); $ content [] = array (" Title "=>" Multi-Image 3 Title "," Description "=> "", "PicUrl" => "http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" => "http://m.cnblogs.com /? U = txw1958 "); $ result = $ this-> transmitNews ($ object, $ content);} else if ($ keyword =" Music ") {// reply to music message $ content = array ("Title" => "most dazzling ethnic style", "Description" => "singer: Phoenix Legend ", "MusicUrl" => "http: // 121.199.4.61/music/zxmzf.mp3", "HQMusicUrl" => "http: // 121.199.4.61/music/zxmzf.mp3 "); $ result = $ this-> transmitMusic ($ object, $ content);} return $ result;} private function receiveImage ($ object) {// reply to image message $ content = ar Ray ("MediaId" => $ object-> MediaId); $ result = $ this-> transmitImage ($ object, $ content); return $ result ;} private function implements evoice ($ object) {// returns a voice message $ content = array ("MediaId" => $ object-> MediaId ); $ result = $ this-> transmitVoice ($ object, $ content); return $ result;} private function receiveVideo ($ object) {// reply to the video message $ content = array ("MediaId" => $ object-> MediaId, "ThumbMediaId" => $ object-> ThumbMed IaId, "Title" => "", "Description" => ""); $ result = $ this-> transmitVideo ($ object, $ content );; return $ result;}/** reply to the text message, and wrap the xml message to be replied. */Private function transmitText ($ object, $ content) {$ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content); // The function sprintf () is very interesting. You can search for it. Return $ result;}/** reply Image message */private function transmitImage ($ object, $ imageArray) {$ itemTpl = "<Image> <MediaId> <! [CDATA [% s]> </MediaId> </Image> "; $ item_str = sprintf ($ itemTpl, $ imageArray ['mediaid ']); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [image]> </MsgType> $ item_str </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result;}/** reply voice message */private function transmitVoice ($ object, $ voiceArray) {$ itemTpl = "<Voice> <MediaId> <! [CDATA [% s]> </MediaId> </Voice> "; $ item_str = sprintf ($ itemTpl, $ voiceArray ['mediaid ']); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [voice]> </MsgType> $ item_str </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result;}/** reply to video message */private function transmitVideo ($ object, $ videoArray) {$ itemTpl = "<Video> <MediaId> <! [CDATA [% s]> </MediaId> <ThumbMediaId> <! [CDATA [% s]> </ThumbMediaId> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> </Video> "; $ item_str = sprintf ($ itemTpl, $ videoArray ['mediaid '], $ videoArray ['thumbmediaid '], $ videoArray ['title'], $ videoArray ['description']); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [video]> </MsgType> $ item_str </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result;}/** reply to text message */private function transmitNews ($ object, $ arr_item) {if (! Is_array ($ arr_item) return; $ itemTpl = "<item> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <PicUrl> <! [CDATA [% s]> </PicUrl> <Url> <! [CDATA [% s]> </Url> </item> "; $ item_str =" "; foreach ($ arr_item as $ item) $ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']); $ newsTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> <Content> <! [CDATA []> </Content> <ArticleCount> % s </ArticleCount> <Articles> $ item_str </Articles> </xml> "; $ result = sprintf ($ newsTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ arr_item); return $ result ;} /** reply Music message */private function transmitMusic ($ object, $ musicArray) {$ itemTpl = "<Music> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <MusicUrl> <! [CDATA [% s]> </MusicUrl> <HQMusicUrl> <! [CDATA [% s]> </HQMusicUrl> </Music> "; $ item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [music]> </MsgType> $ item_str </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;}}?>

3. Interface

3.1 What is the interface

An interface is equivalent to a tool with specific functions. For example, if you need to drill holes on the wall when building a house, you will use a drilling rig tool to drill holes. From tools to drilling, you need to complete a series of steps such as power insertion, calibration, and drilling to achieve your goal. Drilling Rig is our interface, and power insertion, calibration, and drilling are the steps for us to call the tool to complete our purpose.

For example.

To call an API, follow these steps:
1. Obtain the connection address of the menu interface and establish a dialogue with the interface through the curl function.
2. Send the menu creation data to this interface.
After the interface is called, the interface automatically processes the data and generates a menu on the public page.

For details about how to call an API, see the following chapter: public platform development (3): Call an advanced API.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.