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 a small program

WBOY
WBOYOriginal
2023-07-08 15:19:362321browse

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.

  1. Build a PHP development environment
    First, we need to build a PHP development environment locally. It is recommended to use integrated development environments such as XAMPP or WAMP, which can install PHP, MySQL and Apache servers with one click, and provide convenient management tools.
  2. Create database
    Using PHP to develop the backend interface of the applet usually requires database support. Use MySQL or other relational database to store data. Create a database through management tools and create corresponding table structures.
  3. Write PHP interface code
    Create a PHP file and name it api.php. In this file, we can write various interface functions to handle the mini program's requests and return data.

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.

  1. Deploy PHP interface
    Upload the written api.php file to the server running PHP to ensure that the server environment supports PHP operation. You can put this file into the htdocs folder of the XAMPP or WAMP installation directory, and then access this file through a URL, such as http://localhost/api.php.
  2. Call the interface in the mini program
    In the mini program, call the PHP interface through the wx.request method and pass the corresponding parameters. The following is a simple applet sample code that shows how to call the interface:
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!

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