How to use PHP to develop the online discussion function of WeChat applet?
With the popularity of WeChat mini programs, more and more developers are paying attention to how to add online discussion functions to mini programs. This article will introduce how to use PHP to develop the online discussion function of WeChat applet and provide specific code examples.
1. Preparation work
Before we start, we need to prepare the following work:
2. Create a database table
First, we need to create a table in the database to store discussion data. Suppose our table name is "discussions" and contains the following fields:
You can use the following SQL statement to create a table:
CREATE TABLE discussions
(
id
int(11) NOT NULL AUTO_INCREMENT,
openid
varchar(100) NOT NULL,
content
text NOT NULL,
create_time
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3. Mini program side code
In the mini program side, we You need to use the API provided by WeChat to send discussion content to the server. Assume that the code of our discussion page is as follows:
<!-- discusson.wxml --> <view> <textarea bindinput="onInput"></textarea> <button bindtap="onSubmit">提交讨论</button> </view>
// discussion.js Page({ data: { content: '', // 讨论内容 }, onInput: function(event) { this.setData({ content: event.detail.value, }); }, onSubmit: function() { wx.request({ url: 'https://your-server-url/discussion-api.php', method: 'POST', data: { content: this.data.content, }, success: function() { wx.showToast({ title: '提交成功', }); }, }); }, });
4. Server-side code
On the server side, we need to write a PHP script to process the request sent by the applet and store the discussion data Store in database. Suppose we save the PHP script as "discussion-api.php", the specific code is as follows:
// discussion-api.php <?php header('Content-Type: application/json'); // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); if ($conn->connect_error) { die('数据库连接失败:' . $conn->connect_error); } // 获取小程序发送的讨论内容 $content = $_POST['content']; // 获取小程序发送的用户OpenID $openid = isset($_SERVER['HTTP_OPENID']) ? $_SERVER['HTTP_OPENID'] : ''; // 插入讨论数据到数据库中 $sql = "INSERT INTO discussions (openid, content) VALUES ('$openid', '$content')"; if ($conn->query($sql) === TRUE) { $response = array('status' => 'success'); } else { $response = array('status' => 'error', 'message' => '插入数据失败:' . $conn->error); } $conn->close(); echo json_encode($response); ?>
It should be noted that we use the HTTP_OPENID header on the server side to obtain the user OpenID sent by the applet. You can add this header in the request of the mini program, for example:
wx.request({ url: 'https://your-server-url/discussion-api.php', header: { 'openid': '用户的OpenID', }, // ... });
5. Test the discussion function
At this point, we have completed the development of the online discussion function of the WeChat mini program using PHP code. In order to test the discussion function, you can test the function of submitting discussions on the mini program, and then check whether the discussion data is stored correctly in the database.
6. Summary
Through the introduction of this article, we have learned how to use PHP to develop the online discussion function of WeChat applet. We implemented the online discussion function by sending discussion content on the mini program side, and then storing the discussion data in the database on the server side. Hope this article is helpful to you!
The above is the detailed content of How to use PHP to develop the online discussion function of WeChat applet?. For more information, please follow other related articles on the PHP Chinese website!