WeChat public platform development function integration

高洛峰
Release: 2017-03-06 09:27:10
Original
1510 people have browsed it

1. Introduction

#In the previous WeChat function development documents, the functions of each WeChat are independent. A single WeChat can only provide one function, which does not meet the needs of mass developers and customers. Therefore, in this article, we will briefly integrate the WeChat functions developed previously for readers’ reference.

2. Idea analysis

A simple way is to intercept keywords, then judge and execute Corresponding function code. This approach is more suitable for simple WeChat with few functions; another approach is to number each function and then record the function status selected by the user. Every time the user queries, first determine his status and then execute the corresponding function code. . This approach is suitable for WeChat that integrates many complex functions; developers can choose according to their own needs. In this article, we will explain the integration of weather and translation functions. The integration of more functions is similar. You can refer to it.

3. Keyword Interception Practice

##3.1 Keyword Interception

We define that the format of the message sent by the user is fixed. The weather query format is "region + weather", such as "Suzhou weather", "Beijing weather", so first intercept the last two words to determine whether it is "weather" keyword, and then intercept the previous city name to query. In the same way, translation also intercepts the first two words to determine whether they are the "translation" keyword, and then intercepts the following text for query operation.

//截取关键字 $weather_key = mb_substr($keyword,-2,2,"UTF-8"); $city_key = mb_substr($keyword,0,-2,"UTF-8"); $translate_key = mb_substr($keyword,0,2,"UTF-8"); $word_key = mb_substr($keyword,2,200,"UTF-8");
Copy after login

3.2 Function Integration

if($weather_key == '天气' && !empty($city_key) && $translate_key != '翻译'){ $contentStr = _weather($city_key); }elseif($translate_key == '翻译' && !empty($word_key)){ $contentStr = _baiduDic($word_key); }else{ $contentStr = "感谢您关注【卓锦苏州】\n微信号:zhuojinsz"; }
Copy after login

Instructions:in Here, we have encapsulated weather query and translation into functions _weather() and _baiduDic(), and then imported these files and called them directly here, which is very convenient.

In this way, we have completed the integration of weather and translation functions.

3.3 Test

WeChat public platform development function integration

The test was successful.

4. Status Recording Practice

4.1 Description

First, we need to Number, for example:

Reply serial number:

1. Weather query

2. Translation query

Then use the database to record the user's query status, the user Each time a message is entered, the system first queries the user's status from the database and then performs corresponding operations.

4.2 Create the user status table user_flags.

-- -- 表的结构 `user_flags` -- CREATE TABLE IF NOT EXISTS `user_flags` ( `from_user` varchar(50) NOT NULL, `flag_id` int(4) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Copy after login

WeChat public platform development function integration

4.3 Introducing database function files

To operate the database, you need to introduce data operation files. The MySQL cloud database provided by BAE is used here.

//引入数据库文件 require_once('includes/mysql_bae.func.php');
Copy after login

4.4 Determine user status

//判断用户状态 $sql = "SELECT flag_id FROM user_flags WHERE from_user = '$fromUsername' LIMIT 0,1"; $result = _select_data($sql); while (!!$rows = mysql_fetch_array($result)) { $user_flag = $rows[flag_id]; }
Copy after login

Description:Get the flag_id from the user_flags table and assign it to $user_flag for the following judgment operation.

4.5 Determine the user’s existing status and the new input status

if(trim($keyword) <> $user_flag && is_numeric($keyword)) { $user_flag = ''; $sql = "DELETE FROM user_flags WHERE from_user = '$fromUsername'"; _delete_data($sql); }
Copy after login

Instructions:Determine the user's existing status and the newly entered status. If the status is different and the entered keyword is a number, set $user_flag to empty and clear the status in the database, as if it were the first query process

4.6 User status judgment

A.The status is empty, that is, the first query

if (empty($user_flag)) { switch ($keyword) { case 1: //查询天气 $sql = "insert into user_flags(from_user,flag_id) values('$fromUsername','1')"; $contentStr = "请输入要查询天气的城市:如北京、上海、苏州"; break; case 2: //翻译 $sql = "insert into user_flags(from_user,flag_id) values('$fromUsername','2')"; $contentStr = "请输入要翻译的内容:如:早上好、good morning、おはよう"; break; default: //其他 $sql = ""; $contentStr = "感谢您关注【卓锦苏州】\n微信号:zhuojinsz\n请回复序号:\n1. 天气查询\n2. 翻译查询\n输入【帮助】查看提示\n更多内容,敬请期待..."; break; } //判断并执行上面的插入语句 if (!empty($sql)) { _insert_data($sql); } }
Copy after login

Description:The user status is empty, that is, the first query. If the keyword entered by the user is the function serial number, that is, 1 or 2, the user status is written to the database, and then a prompt message is given; if the user enters If the keyword is not a function serial number, help information will be given to prompt the user to input.

B.The user status is not empty

else{ if ($user_flag == '1') { $contentStr = _weather($keyword); //查询天气 }elseif ($user_flag == '2') { $contentStr = _baiduDic($keyword); //翻译 } }
Copy after login

Explanation:The user status is not empty If empty, the user has already performed a query operation. As long as the user does not switch functions, it will remain under the existing function and execute the corresponding code.

4.7 Test

WeChat public platform development function integration

For more articles related to WeChat public platform development function integration, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!