The public platform provides three message reply formats: text reply, music reply, and text reply. in this article, we will briefly explain the three message reply formats and encapsulate them into functions for your use.
I. INTRODUCTION
The public platform provides three message reply formats: text reply, music reply, and text reply. in this article, we will briefly explain the three message reply formats and encapsulate them into functions for your use.
II. train of thought analysis
For each POST request, the developer returns a specific xml structure in the response packet to respond to the message (now supports text, text, voice, video, music ).
3. text Reply
3.1 text reply xml structure
toUser
fromUser
12345678
text
content
3.2 Structure Description
3.5 encapsulated into callable functions
We can encapsulate the above content as a function and directly call it where the response text is needed, which is convenient and concise. The responseText. func. inc. php code is as follows.
function _response_text($object,$content){ $textTpl = "
%s
%s
%s
text
%s
%d
"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr;}
In this way, you only need to input $ object and $ content, introduce the file into the file that needs to reply to the text, and then call the _ response_text () method to directly reply to the text.
3.6 test code
3.6.1 introduce the function file that replies to text in the main file
require_once 'responseText.func.inc.php';
3.6.2 normal message Reply
Public function handleText ($ postObj) {$ keyword = trim ($ postObj-> Content); if (! Empty ($ keyword) {$ contentStr = "public platform-source code of the text reply function"; // $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); $ resultStr = _ response_text ($ postObj, $ contentStr); echo $ resultStr;} else {echo "Input something... ";}}
3.6.3 reply when paying attention
Public function handleEvent ($ object) {$ contentStr = ""; switch ($ object-> Event) {case "subscribe ": $ contentStr = "thank you for choosing zhuojin Suzhou ]". "\ n ". "No.: zhuojinsz"; break; default: $ contentStr = "Unknow Event :". $ object-> Event; break;} $ resultStr = _ response_text ($ object, $ contentStr); return $ resultStr ;}
3.7 test results
The text is successfully replied.
IV. graphic Reply
4.1 text-and-text reply xml structure
toUser
fromUser
12345678
news
2
<![CDATA[title1]]>
description1
picurl
url
<![CDATA[title]]>
description
picurl
url
4.2 Structure Description
Similar to the text reply format, you only need to fill in the corresponding content in the corresponding position to reply to the text information.
4.3 Implementation
The text and text reply can be a single text or a multi-Text text. here we first use the case of a single text to guide the reader, and then lead to multiple text and text.
We break down the xml structure of the reply text into the following three structures: the text header, the text body, and the text tail. the text body is the title, description, image URL and original URL.
$newsTplHead = "
%s
%s
%s
news
1 ";$newsTplBody = "
<![CDATA[%s]]>
%s
%s
%s
";$newsTplFoot = "
0
";
Next, we insert the corresponding content for the three-segment structure:
A. $ newsTplHead
$header = sprintf($newsTplHead, $object->FromUserName, $object->ToUserName, time());
B. $ newsTplBody
$title = $newsContent['title'];$desc = $newsContent['description'];$picUrl = $newsContent['picUrl'];$url = $newsContent['url'];$body = sprintf($newsTplBody, $title, $desc, $picUrl, $url);
Note:$ NewsContent is an array of input functions from the main file.
C. $ newsTplFoot
$FuncFlag = 0;$footer = sprintf($newsTplFoot, $FuncFlag);
After splicing the three sections, you can return a single image.
return $header.$body.$footer;
Write the above content into a function and name it the _ response_news () function for the following call to test.
4.4 test code
4.4.1 introduce the function file that replies to text in the main file
require_once 'responseNews.func.inc.php';
4.4.2 create an array and input
In the main file, you only need to input an array and $ postObj to the _ response_news () function.
$ Record = array ('title' => 'shantystreet ', 'description' => 'from the east of shantystreet, the wangheng Bridge at Tuen Mun crossing, and the Wangshan bridge from the West to the scenic spot in Suzhou, with a length of about seven miles, so the Suzhou saying goes, "seven miles from mountain pond to Tiger Hill "... ', 'picurl' => 'http: // think#.duapp.com/images/suzhou.jpg', 'URL' => 'http: // mp.weixin.qq.com/mp/appmsg/show? _ Biz = MjM5NDM0NTEyMg = & appmsgid = 10000046 & itemidx = 1 & sign = Alibaba # wechat_redirect '); $ resultStr = _ response_news ($ postObj, $ record ); echo $ resultStr;
4.5 test results
foreach($newsContent as $key => $value){ $body .= sprintf($newsTplBody, $value['title'], $value['description'], $value['picUrl'], $value['url']);}
Note:$ NewsContent is an array of input functions from the main file.
4.6.4 splicing and returning
return $header.$body.$footer;
Write the preceding content to a function and name it _ response_multiNews () for the following call and test.
4.7 test multiple images
4.7.1 introduce a function file that replies to multiple texts into the main file
require_once 'responseMultiNews.func.inc.php';
4.7.2 create a multi-dimensional array and input
$ Record [0] = array ('title' => 'guanqian street ', 'description' => 'guanqian Street is located in Suzhou city, Jiangsu province. it is a century-old commercial street in the Qing Dynasty, the street is filled with famous stores, and the name is widely spoken at home and abroad... ', 'picurl' => 'http: // joythink.duapp.com/images/suzhou.jpg', 'URL' => 'http: // mp.weixin.qq.com/mp/appmsg/show? _ Biz = MjM5NDM0NTEyMg = & appmsgid = 10000052 & itemidx = 1 & sign = 90518631fd3e85dd1fde7f77c04e44d5 # wechat_redirect ');...... $ record [11] = array ('title' => 'pingjiang Road', 'description' => 'pingjiang Road is located in the northeast of the ancient city of Suzhou. it is a path of Luohe River, it is a classic Water Lane with a long history in Suzhou. Suzhou, also known as Pingjiang in Songyuan, uses this path... ', 'picurl' => 'http: // scheme, 'URL' => 'http: // mp.weixin.qq.com/mp/appmsg/show? _ Biz = MjM5NDM0NTEyMg ==& appmsgid = 10000056 & itemidx = 1 & sign = Alibaba # wechat_redirect '); $ resultStr = _ response_multiNews ($ postObj, $ record ); echo $ resultStr;
4.8 multi-image testing results
toUser
fromUser
12345678
music
<![CDATA[TITLE]]>
DESCRIPTION
MUSIC_Url
HQ_MUSIC_Url
5.2 Structure Description
<![CDATA[Far Away From Home]]>
Groove Coverage
http://thinkshare.duapp.com/music/10001.mp3
http://thinkshare.duapp.com/music/10001.mp3
5.3.2 test code
$resultStr = _response_music($postObj,$keyword);echo $resultStr;
5.3.3 test results
Create table if not exists 'tbl _ music' ('Music _ id' int (11) not null, 'Music _ name' varchar (40) not null, 'Music _ singer' varchar (40) not null, 'Music _ lrc 'text not null, primary key ('Music _ id') ENGINE = MyISAM default charset = utf8; insert into 'tbl _ music' ('Music _ id', 'Music _ name', 'Music _ singer', 'Music _ lrc ') VALUES (10001, 'far Away From home', 'Groove coverage', 'far away from home'), (10002, 'The Dawn ', 'dreamweight', 'The dawn '), (20002, 'Miss Dong ', 'Song Dongye', 'Miss Dong '), (20001, 'left', 'Yang xinline', 'left ');
5.4.2 _ response_music () function compilation
A. introduce database operation files
require_once('mysql_bae.func.php');
B. database operations and data processing
$query = "SELECT * FROM tbl_music WHERE music_name LIKE '%$musicKeyword%'";$result = _select_data($query);$rows = mysql_fetch_array($result, MYSQL_ASSOC);$music_id = $rows[music_id];
Note:$ MusicKeyword is the key word of the song name passed in from the main file. here, fuzzy query is used to retrieve only the first data.
C. determine whether the query is successful
if($music_id <> ''){ $music_name = $rows[music_name]; $music_singer = $rows[music_singer]; $musicUrl = "http://thinkshare.duapp.com/music/".$music_id.".mp3"; $HQmusicUrl = "http://thinkshare.duapp.com/music/".$music_id.".mp3"; $resultStr = sprintf($musicTpl, $object->FromUserName, $object->ToUserName, time(), $music_name, $music_singer, $musicUrl, $HQmusicUrl); return $resultStr;}else{ return ""; }
Note:If the song information is queried, data is returned according to the xml structure. If no information is found, null is returned for the judgment of the main file.
Encapsulate the above code into the _ response_music () function and save it as the responseMusic. func. inc. php file for the main file to call.
5.4.3 test code
A. introduce function files for replying music and text
// Introduce the function file require_once 'responsemusic. func. inc. php' for music reply; // introduce the function file require_once 'responsetext. func. inc. php' for text reply ';
B. Call
If (! Empty ($ keyword) {$ resultStr = _ response_music ($ postObj, $ keyword); if ($ resultStr <> '') {echo $ resultStr ;} else {echo _ response_text ($ postObj, "No [" found 【". $ keyword. "] song information! ");}}
Note:If the song information is queried, the obtained information is returned. If no information is found, the _ response_text () function is called to return the text information.
5.5 simulated karaoke test
The music reply test is successful.
For more articles about public platform development message reply summary, please follow PHP Chinese network!