Your Cart

Loading cart...

API-Based Website Builder (Android + PHP)

Modern applications no longer depend on traditional websites alone. Most mobile apps and modern web apps use API-based architecture, where Android apps communicate with a backend server using APIs. This guide explains how to build an API-based system using Android + PHP + MySQL.


1. What Is an API-Based System?

An API (Application Programming Interface) allows one application to communicate with another. In an API-based system:

The frontend never accesses the database directly. All data flows through secure APIs.


2. Why Use API-Based Architecture?

This architecture is used by most modern platforms.


3. Basic Architecture Overview

Android App
   ↓ (HTTP / HTTPS)
PHP REST API
   ↓
MySQL Database

The Android app sends requests. PHP processes them and returns JSON responses.


4. Preparing the Backend (PHP)

Create an API folder inside your project:

/api
 ├─ config.php
 ├─ login.php
 ├─ register.php
 ├─ posts.php
 └─ response.php

All API files should return JSON only.


5. Database Setup

Example users table:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(150) UNIQUE,
    password VARCHAR(255),
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Example posts table:

CREATE TABLE posts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    content TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

6. API Database Connection (config.php)

<?php
header("Content-Type: application/json");

$conn = new mysqli("localhost", "root", "", "api_example");

if ($conn->connect_error) {
    echo json_encode([
        "status" => false,
        "message" => "Database connection failed"
    ]);
    exit;
}
?>

7. User Registration API

<?php
include 'config.php';

$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';

$hash = password_hash($password, PASSWORD_DEFAULT);

$stmt = $conn->prepare(
    "INSERT INTO users (name, email, password) VALUES (?, ?, ?)"
);
$stmt->bind_param("sss", $name, $email, $hash);

if ($stmt->execute()) {
    echo json_encode(["status" => true, "message" => "Registered"]);
} else {
    echo json_encode(["status" => false, "message" => "Error"]);
}
?>

8. Login API

<?php
include 'config.php';

$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';

$stmt = $conn->prepare(
    "SELECT id, password FROM users WHERE email=?"
);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    if (password_verify($password, $row['password'])) {
        echo json_encode(["status" => true, "user_id" => $row['id']]);
    } else {
        echo json_encode(["status" => false, "message" => "Invalid password"]);
    }
} else {
    echo json_encode(["status" => false, "message" => "User not found"]);
}
?>

9. Fetching Data API (posts.php)

<?php
include 'config.php';

$result = $conn->query("SELECT * FROM posts ORDER BY id DESC");
$data = [];

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

echo json_encode([
    "status" => true,
    "posts" => $data
]);
?>

10. Android App Communication

The Android app sends HTTP requests using libraries such as:

Example API URL:

https://example.com/api/login.php

Android parses JSON responses and updates UI.


11. API Security Best Practices

Security is critical for mobile applications.


12. API Versioning

For long-term maintenance, use API versions:

/api/v1/login.php
/api/v1/posts.php

This prevents breaking older apps.


13. Testing APIs

APIs can be tested using:

Always test before deploying.


Conclusion

API-based architecture is the foundation of modern applications. By combining Android with PHP and MySQL, you can build scalable, secure, and reusable backend systems.

Once your API is stable, you can reuse it for websites, mobile apps, and other platforms without rewriting backend logic.

💬