Home > Article > Backend Development > How to use PHP to implement the online evaluation function of WeChat applet?
How to use PHP to implement the online evaluation function of WeChat mini program?
WeChat Mini Program has become one of the preferred platforms for many companies and developers to promote products and services. Among them, implementing the online assessment function is one of the common requirements in mini programs. This article will introduce how to use PHP to write back-end code to implement the online evaluation function in the WeChat applet, and provide specific code examples.
First of all, we need to configure the server domain name in the background of the mini program, and obtain the AppID and AppSecret of the mini program in order to subsequently call the official WeChat API.
Next, we will implement the online assessment function in three steps: user login, obtaining questions and submitting answers.
<?php $appid = 'YOUR_APPID'; $secret = 'YOUR_APP_SECRET'; $code = $_GET['code']; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); $data = json_decode($result, true); $openid = $data['openid']; $session_key = $data['session_key']; ?>
The following is a sample code to obtain the question:
<?php $servername = 'YOUR_DB_SERVER'; $username = 'YOUR_DB_USERNAME'; $password = 'YOUR_DB_PASSWORD'; $dbname = 'YOUR_DB_NAME'; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = 'SELECT * FROM questions'; $result = $conn->query($sql); if ($result->num_rows > 0) { $questions = array(); while ($row = $result->fetch_assoc()) { $questions[] = $row; } // 返回题目数据给小程序端 echo json_encode($questions); } else { echo 'No questions found.'; } $conn->close(); ?>
The following is a sample code for saving answers:
<?php $servername = 'YOUR_DB_SERVER'; $username = 'YOUR_DB_USERNAME'; $password = 'YOUR_DB_PASSWORD'; $dbname = 'YOUR_DB_NAME'; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $openid = $_GET['openid']; $answers = $_POST['answers']; $sql = 'INSERT INTO answers (openid, answers) VALUES (' . $openid . ', ' . $answers . ')'; if ($conn->query($sql) === TRUE) { echo 'Answers saved successfully.'; } else { echo 'Error: ' . $sql . '<br>' . $conn->error; } $conn->close(); ?>
The above are the steps and code examples for using PHP to implement the online evaluation function of the WeChat applet. Of course, actual development may also involve some other functions and details, which need to be adjusted according to specific needs. I hope this article can help you, and I wish you smooth implementation of the online evaluation function in the process of developing WeChat mini programs!
The above is the detailed content of How to use PHP to implement the online evaluation function of WeChat applet?. For more information, please follow other related articles on the PHP Chinese website!