亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet WeChat Development Summary and introduction of WeChat public platform development message reply

Summary and introduction of WeChat public platform development message reply

Mar 06, 2017 am 09:38 AM
WeChat development

1. Introduction

WeChat public platform provides three message reply formats, namely text reply, music reply and image reply. Text reply, in this article, we will briefly explain the formats of these three message replies, and then encapsulate them into functions for readers to use.

2. Idea analysis

For each POST request, the developer returns a specific xml structure in the response package , respond to the message (now supports reply text, graphics, voice, video, music).

3. Text reply

3.1 Text reply xml structure

 <xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[content]]></Content>
 </xml>

3.2 Structure description

Summary and introduction of WeChat public platform development message reply

3.3 Specific implementation

For the xml structure given above, we only need to fill in the content in the corresponding position, and then format the output. That's it.

Summary and introduction of WeChat public platform development message reply

Explanation:

ToUserName is filled in with $fromUsername = $postObj->FromUserName, which is to return the message to The user who sent the message is the receiver’s account.

FromUserName is filled in with $toUsername = $postObj->ToUserName, which is the developer’s WeChat account.

This is the official text reply. You only need to instantiate its responseMsg() method to reply to the "Welcome to wechat world!" message.

Here we make a slight modification and return fromUsername and toUsername messages to facilitate readers to understand the above instructions.

Summary and introduction of WeChat public platform development message reply

3.4 Test results

Summary and introduction of WeChat public platform development message reply

3.5 Encapsulate into a callable function

We can convert the above The content is encapsulated into a function and can be called directly where the reply text is needed. It is convenient and concise. The code of responseText.func.inc.php is as follows.

function _response_text($object,$content){
    $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>%d</FuncFlag>
                </xml>";
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
    return $resultStr;
}

In this way, as long as you pass in $object and $content, then introduce the file into the file that needs to reply to the text, and then call the _response_text() method, you can directly reply to the text.

3.6 Test code

3.6.1 Introduce the function file of reply text into the main file

require_once &#39;responseText.func.inc.php&#39;;

3.6.2 Ordinary message reply

public function handleText($postObj)
    {
        $keyword = trim($postObj->Content);

        if(!empty( $keyword ))
        {
            $contentStr = "微信公眾平臺(tái)-文本回復(fù)功能源代碼";
            //$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            $resultStr = _response_text($postObj,$contentStr);
            echo $resultStr;
        }else{
            echo "Input something...";
        }
}

3.6.3 Reply when following

public function handleEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "感謝您關(guān)注【卓錦蘇州】"."\n"."微信號(hào):zhuojinsz";
                break;
            default :
                $contentStr = "Unknow Event: ".$object->Event;
                break;
        }
        $resultStr = _response_text($object, $contentStr);
        return $resultStr;
}

3.7 Test result

Summary and introduction of WeChat public platform development message reply

The reply text was successful.

4. Graphic reply

4.1 Graphic reply xml structure

 <xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[news]]></MsgType>
 <ArticleCount>2</ArticleCount>
 <Articles>
 <item>
 <Title><![CDATA[title1]]></Title> 
 <Description><![CDATA[description1]]></Description>
 <PicUrl><![CDATA[picurl]]></PicUrl>
 <Url><![CDATA[url]]></Url>
 </item>
 <item>
 <Title><![CDATA[title]]></Title>
 <Description><![CDATA[description]]></Description>
 <PicUrl><![CDATA[picurl]]></PicUrl>
 <Url><![CDATA[url]]></Url>
 </item>
 </Articles>
 </xml>

4.2 Structure description

Summary and introduction of WeChat public platform development message reply

Similar to the text reply format, you only need to fill in the corresponding content in the corresponding position to reply to the graphic message.

4.3 Specific Implementation

The picture-text reply can be a single picture-text or multiple pictures-text. Here we first guide the readers with the case of single picture-text, and then introduce the multi-picture-text .

We decompose the xml structure of the image and text reply into the following three structures, the image and text header, the image and text body, and the image and text tail. The image and text body is the title, description, and image URL that you see when replying to the image and text. and original URL.

$newsTplHead = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[news]]></MsgType>
                <ArticleCount>1</ArticleCount>
                <Articles>";
$newsTplBody = "<item>
                <Title><![CDATA[%s]]></Title> 
                <Description><![CDATA[%s]]></Description>
                <PicUrl><![CDATA[%s]]></PicUrl>
                <Url><![CDATA[%s]]></Url>
                </item>";
$newsTplFoot = "</Articles>
                <FuncFlag>0</FuncFlag>
                </xml>";

Next, we insert corresponding content into the three structures:

A. $newsTplHead

$header = sprintf($newsTplHead, $object->FromUserName, $object->ToUserName, time());

B. $newsTplBody

$title = $newsContent[&#39;title&#39;];
$desc = $newsContent[&#39;description&#39;];
$picUrl = $newsContent[&#39;picUrl&#39;];
$url = $newsContent[&#39;url&#39;];
$body = sprintf($newsTplBody, $title, $desc, $picUrl, $url);

Explanation: $newsContent is the image and text array passed into the function from the main file.

C. $newsTplFoot

$FuncFlag = 0;
$footer = sprintf($newsTplFoot, $FuncFlag);

Then you can reply to a single image and text by splicing the three paragraphs back together.

return $header.$body.$footer;

Write the above content into a function and name it _response_news() function for the following call test.

4.4 Test code

4.4.1 Introduce the function file for replying to the image and text in the main file

require_once &#39;responseNews.func.inc.php&#39;;

4.4.2 Create an array and pass it in

In the main file, you only need to pass an array and $postObj to the _response_news() function.

$record=array(
&#39;title&#39; =>&#39;山塘街&#39;,
&#39;description&#39; =>&#39;山塘街東起閶門渡僧橋,西至蘇州名勝虎丘山的望山橋,長約七里,所以蘇州俗語說“七里山塘到虎丘”...&#39;,
&#39;picUrl&#39; => &#39;http://thinkshare.duapp.com/images/suzhou.jpg&#39;,
&#39;url&#39; =>&#39;http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000046&itemidx=1&sign=9e7707d5615907d483df33ee449b378d#wechat_redirect&#39;
);

$resultStr = _response_news($postObj,$record);
echo $resultStr;
4.5 測(cè)試結(jié)果

Summary and introduction of WeChat public platform development message reply

點(diǎn)擊進(jìn)入查看

Summary and introduction of WeChat public platform development message reply

單圖文回復(fù)測(cè)試成功。

4.6 多圖文回復(fù)

有了上面的引導(dǎo),讀者應(yīng)該能夠想到回復(fù)多圖文的思路了,就是將多維數(shù)組中的值循環(huán)放到相應(yīng)的位置,然后拼接起來就可以了,下面進(jìn)行講解。

4.6.1 獲取圖文條數(shù)


$bodyCount = count($newsContent);


4.6.2 判斷圖文條數(shù)

因?yàn)槲⑿畔拗屏嘶貜?fù)的圖文消息數(shù)為10條以內(nèi),所以需要判斷圖文條數(shù),如果小于10條,則圖文數(shù)等于原來的圖文數(shù),如果大于等于10條,則強(qiáng)制限制為10條。


$bodyCount = $bodyCount < 10 ? $bodyCount : 10;


4.6.3 組織圖文體

圖文頭和圖文尾和上面單圖文一樣,不再贅述,主要是圖文體的組織。

用foreach 循環(huán)出數(shù)組的內(nèi)容并賦予圖文體,并進(jìn)行拼接:


foreach($newsContent as $key => $value){
    $body .= sprintf($newsTplBody, $value[&#39;title&#39;], $value[&#39;description&#39;], $value[&#39;picUrl&#39;], $value[&#39;url&#39;]);
}


說明:$newsContent 是從主文件傳入函數(shù)的圖文數(shù)組。

4.6.4 拼接并返回


return $header.$body.$footer;


將以上內(nèi)容寫到一個(gè)函數(shù)里,命名為 _response_multiNews() 函數(shù),以供下面調(diào)用測(cè)試。

4.7 測(cè)試多圖文

4.7.1 在主文件中引入回復(fù)多圖文的函數(shù)文件


require_once &#39;responseMultiNews.func.inc.php&#39;;


4.7.2 創(chuàng)建多維數(shù)組并傳入

$record[0]=array(
    &#39;title&#39; =>&#39;觀前街&#39;,
    &#39;description&#39; =>&#39;觀前街位于江蘇蘇州市區(qū),是成街于清朝時(shí)期的百年商業(yè)老街,街上老店名店云集,名聲遠(yuǎn)播海內(nèi)外...&#39;,
    &#39;picUrl&#39; => &#39;http://joythink.duapp.com/images/suzhou.jpg&#39;,
    &#39;url&#39; =>&#39;http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000052&itemidx=1&sign=90518631fd3e85dd1fde7f77c04e44d5#wechat_redirect&#39;
);
......
$record[11]=array(
    &#39;title&#39; =>&#39;平江路&#39;,
    &#39;description&#39; =>&#39;平江路位于蘇州古城東北,是一條傍河的小路,北接拙政園,南眺雙塔,全長1606米,是蘇州一條歷史攸久的經(jīng)典水巷。宋元時(shí)候蘇州又名平江,以此名路...&#39;,
    &#39;picUrl&#39; => &#39;http://joythink.duapp.com/images/suzhouScenic/pingjianglu.jpg&#39;,
    &#39;url&#39; =>&#39;http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000056&itemidx=1&sign=ef18a26ce78c247f3071fb553484d97a#wechat_redirect&#39;
);
$resultStr = _response_multiNews($postObj,$record);
echo $resultStr;

4.8 測(cè)試多圖文結(jié)果

Summary and introduction of WeChat public platform development message reply

點(diǎn)擊進(jìn)入查看

Summary and introduction of WeChat public platform development message reply

測(cè)試多圖文成功。

五、音樂回復(fù)

微信還提供了一種消息回復(fù)的格式,即音樂回復(fù),下面我們編寫程序測(cè)試一下。

注意:由于音樂版權(quán)的問題,現(xiàn)在很少有回復(fù)音樂的API,開放的API 查詢出來的音樂信息也有很多是不正確的。所以在這里,我們上傳幾首音樂到自己的服務(wù)器空間測(cè)試。

本地文件:

Summary and introduction of WeChat public platform development message reply

測(cè)試是否能夠正常播放:

Summary and introduction of WeChat public platform development message reply

5.1 音樂回復(fù)xml 結(jié)構(gòu)


 <xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[music]]></MsgType>
 <Music>
 <Title><![CDATA[TITLE]]></Title>
 <Description><![CDATA[DESCRIPTION]]></Description>
 <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>
 <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>
 </Music>
 </xml>


5.2 結(jié)構(gòu)說明

Summary and introduction of WeChat public platform development message reply

5.3 具體實(shí)施

我們先做一個(gè)固定的歌曲回復(fù)來引導(dǎo)讀者,然后再引出更高級(jí)別的歌曲查詢回復(fù)。

5.3.1 在xml 結(jié)構(gòu)的相應(yīng)位置插入相應(yīng)數(shù)據(jù)


<Music>
<Title><![CDATA[Far Away From Home]]></Title>
<Description><![CDATA[Groove Coverage]]></Description>
<MusicUrl><![CDATA[http://thinkshare.duapp.com/music/10001.mp3]]></MusicUrl>
<HQMusicUrl><![CDATA[http://thinkshare.duapp.com/music/10001.mp3]]></HQMusicUrl>
</Music>

5.3.2 測(cè)試代碼


$resultStr = _response_music($postObj,$keyword);echo $resultStr;


5.3.3 測(cè)試結(jié)果

Summary and introduction of WeChat public platform development message reply

5.4 模擬點(diǎn)歌

有了上面的簡單案例引導(dǎo),讀者應(yīng)該可以想到模擬點(diǎn)歌的具體實(shí)現(xiàn)了吧,下面就來簡單介紹一下。

思路:將歌曲代碼和對(duì)應(yīng)的歌曲名存入數(shù)據(jù)庫,用戶輸入歌曲名,在數(shù)據(jù)庫中找到歌曲名對(duì)應(yīng)的歌曲編號(hào),然后就可以生成MusicUrl 回復(fù)用戶了。

5.4.1 創(chuàng)建數(shù)據(jù)庫

Summary and introduction of WeChat public platform development message reply

建表語句及數(shù)據(jù)文件:


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, &#39;Far Away From Home&#39;, &#39;Groove Coverage&#39;, &#39;far away from home&#39;),
(10002, &#39;The Dawn&#39;, &#39;Dreamtale&#39;, &#39;the dawn&#39;),
(20002, &#39;董小姐&#39;, &#39;宋冬野&#39;, &#39;董小姐&#39;),
(20001, &#39;左邊&#39;, &#39;楊丞琳&#39;, &#39;左邊&#39;);


5.4.2 _response_music() 函數(shù)編寫

A. 引入數(shù)據(jù)庫操作文件


require_once(&#39;mysql_bae.func.php&#39;);


B. 數(shù)據(jù)庫操作及數(shù)據(jù)處理

$query = "SELECT * FROM tbl_music WHERE music_name LIKE &#39;%$musicKeyword%&#39;";
$result = _select_data($query);
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$music_id = $rows[music_id];

注: $musicKeyword 為從主文件傳入的歌曲名關(guān)鍵字,這里使用模糊查詢,只取第一條數(shù)據(jù)。

C. 判斷是否查詢到


if($music_id <> &#39;&#39;)
{
    $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 "";    
}


說明:如果查詢到歌曲信息,按照xml 結(jié)構(gòu)返回?cái)?shù)據(jù);如果未查詢到,則返回空,用于主文件判斷。

將以上代碼封裝成 _response_music() 函數(shù)并保存為responseMusic.func.inc.php 文件供主文件調(diào)用。

5.4.3 測(cè)試代碼

A. 引入回復(fù)音樂和回復(fù)文本的函數(shù)文件


//引入回復(fù)音樂的函數(shù)文件require_once 'responseMusic.func.inc.php';//引入回復(fù)文本的函數(shù)文件require_once &#39;responseText.func.inc.php&#39;;


B. 調(diào)用


if(!empty( $keyword ))
{
    $resultStr = _response_music($postObj,$keyword);
    if($resultStr <> &#39;&#39;)
    {
        echo $resultStr;
    }else
    {
        echo _response_text($postObj,"未查詢到【".$keyword."】的歌曲信息!");    
    }
    
}


說明:如果查詢到歌曲信息,則返回所得信息,如果未查詢到,則調(diào)用 _response_text() 函數(shù)返回文本信息。

5.5 模擬點(diǎn)歌測(cè)試

Summary and introduction of WeChat public platform development message reply

回復(fù)音樂測(cè)試成功。?

更多Summary and introduction of WeChat public platform development message reply相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

?



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

PHP WeChat development: How to implement voting function PHP WeChat development: How to implement voting function May 14, 2023 am 11:21 AM

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

Using PHP to develop WeChat mass messaging tools Using PHP to develop WeChat mass messaging tools May 13, 2023 pm 05:00 PM

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

PHP WeChat development: How to implement customer service chat window management PHP WeChat development: How to implement customer service chat window management May 13, 2023 pm 05:51 PM

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

PHP WeChat development: How to implement user tag management PHP WeChat development: How to implement user tag management May 13, 2023 pm 04:31 PM

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

PHP WeChat development: How to implement group message sending records PHP WeChat development: How to implement group message sending records May 13, 2023 pm 04:31 PM

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

Steps to implement WeChat public account development using PHP Steps to implement WeChat public account development using PHP Jun 27, 2023 pm 12:26 PM

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

How to use PHP for WeChat development? How to use PHP for WeChat development? May 21, 2023 am 08:37 AM

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform

See all articles