Home > Article > Backend Development > How to use PHP to develop the backend interface of a small program
How to use PHP to develop the backend interface of mini programs
In the era of mobile Internet, mini programs have become one of the important platforms for the development of many enterprises and individuals. The development of the backend interface of the mini program is one of the important links in realizing the function of the mini program. This article will introduce how to use PHP to develop the backend interface of the applet and provide code examples.
The following is a simple sample code that shows how to write an interface for verifying user login:
<?php // 连接到数据库 $conn = mysqli_connect("localhost", "root", "", "test"); // 验证用户登录 function login($username, $password) { global $conn; // 查询数据库中是否存在匹配的用户名和密码 $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { // 登录成功 return true; } else { // 登录失败 return false; } } // 处理请求 $action = $_GET['action']; if ($action == 'login') { $username = $_POST['username']; $password = $_POST['password']; // 调用登录函数 $result = login($username, $password); // 返回结果给小程序 if ($result) { echo json_encode(['status' => 1, 'message' => '登录成功']); } else { echo json_encode(['status' => 0, 'message' => '登录失败']); } } // 关闭数据库连接 mysqli_close($conn); ?>
In this sample code, we first connect to the database and define A function login used to authenticate user login. Then, by obtaining the parameters passed by the applet and judging that the request type is login, the login function is called and the corresponding result is returned to the applet.
wx.request({ url: 'http://localhost/api.php?action=login', method: 'POST', data: { username: 'admin', password: '123456' }, success(res) { console.log(res.data) }, fail(err) { console.error(err) } })
In this sample code, we send a POST request to the API address by calling the wx.request method, passing the username and Password parameter. Successful return results will be printed in the console.
Summary
By developing the backend interface of the mini program in PHP, you can achieve rich functions and interactions, making the mini program more flexible and powerful. This article introduces how to use PHP to develop the backend interface of a small program and provides a simple code example. I hope this article can be helpful to your background interface development when developing small programs!
The above is the detailed content of How to use PHP to develop the backend interface of a small program. For more information, please follow other related articles on the PHP Chinese website!