Home >Backend Development >PHP Tutorial >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 applet?

王林
王林Original
2023-10-27 16:30:151035browse

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.

  1. User login
    In the mini program, we can use the WeChat login interface to obtain the user's unique identifier openid and the session key session_key. First, we need to call the wx.login() method on the applet to obtain the temporary login credential code and send it to the backend server. In PHP, we can use the curl library to send an http request and obtain the user's openid and session_key.
<?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'];

?>
  1. Get the question
    On the mini program side, according to the user's openid, we can send a request to the backend server and obtain the corresponding question. Generally speaking, the question data can be stored in the database, and we can use the MySQL database to manage the question data. In PHP, we can use the php-mysql extension to connect to and operate the database.

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();

?>
  1. Submit the answer
    In the mini program, when the user completes the answer, we can send the answer result Go to the backend server for processing. In PHP we can receive and save the user's answers.

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!

Statement:
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