GitHunt
FA

Farrassubahan/php-api-client-server-architecture

Refresh Belajar PHP membangun CRUD lewat API, tidak query langsung.

PHP API Client Architecture (Native)

A lightweight API-driven application built using Native PHP.
This project aims to teach PHP without a framework, demonstrating a clear separation between:

  • API Server โ€“ Handles database operations and returns JSON response
  • Client App โ€“ Consumes API via cURL and renders data in HTML views

The goal is to understand and implement RESTful API architecture,
cURL-based HTTP requests, and modular MVC structure without frameworks.


๐Ÿ”ฅ Features

๐Ÿ–ฅ Client App

  • MVC-like structure (Controller, Views, CurlClient)
  • Consume API via cURL (GET, POST, PUT, DELETE)
  • Modal-based CRUD (Create, Read, Update, Delete)
  • Clean route handling (web.php?action=...)
  • Redirect, form request handling, and dynamic response

๐ŸŒ API Server

  • RESTful API in Native PHP
  • JSON responses with HTTP status codes
  • Endpoint-based structure (users/index.php, users/create.php, etc.)
  • PDO-based database access
  • Password hashing for secure user storage

๐Ÿ“ Project Structure

belajar-php-native/
โ”‚
โ”œโ”€โ”€ api-server/
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ””โ”€โ”€ database.php
โ”‚   โ””โ”€โ”€ endpoints/
โ”‚       โ””โ”€โ”€ users/
โ”‚           โ”œโ”€โ”€ index.php     # GET all users
โ”‚           โ”œโ”€โ”€ show.php      # GET single user
โ”‚           โ”œโ”€โ”€ create.php    # POST - create user
โ”‚           โ”œโ”€โ”€ update.php    # PUT - update user
โ”‚           โ””โ”€โ”€ delete.php    # DELETE - delete user
โ”‚
โ”œโ”€โ”€ client-app/
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ””โ”€โ”€ api_config.php
โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”‚   โ””โ”€โ”€ UsersController.php
โ”‚   โ”œโ”€โ”€ core/
โ”‚   โ”‚   โ””โ”€โ”€ CurlClient.php     # Handles cURL API Requests
โ”‚   โ”œโ”€โ”€ views/
โ”‚   โ”‚   โ””โ”€โ”€ users/
โ”‚   โ”‚       โ”œโ”€โ”€ index.php      # Main UI (CRUD modal-based)
โ”‚   โ”‚       โ”œโ”€โ”€ create.php     # (optional)
โ”‚   โ”‚       โ””โ”€โ”€ edit.php       # (optional)
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ””โ”€โ”€ web.php            # Basic Routing
โ”‚   โ””โ”€โ”€ index.php              # Entry point
โ”‚
โ””โ”€โ”€ README.md

๐Ÿ”Œ Tech Stack

Layer Technology
Client Application Native PHP, HTML, Modal-based UI
API Communication PHP cURL
API Response Format JSON (REST)
Database MySQL (via PDO)
Server Stack Apache (Laragon/XAMPP)

โš™๏ธ Installation & Setup

1๏ธโƒฃ Clone the Repository

git clone https://github.com/your-username/php-api-client-architecture.git
cd php-api-client-architecture

2๏ธโƒฃ Configure API

api-server/config/database.php

$host = "localhost";
$db_name = "your_database";
$username = "root";
$password = "";

3๏ธโƒฃ Configure Client API URL

client-app/config/api_config.php

define("API_BASE_URL", "http://localhost/belajar-php-native/api-server/endpoints/");

4๏ธโƒฃ Ensure cURL is enabled in php.ini

extension=curl

Restart Apache/Laragon after enabling.


๐Ÿš€ Usage

Run API

http://localhost/belajar-php-native/api-server/endpoints/users/index.php

Run Client Application

http://localhost/belajar-php-native/client-app/

Auto loads UsersController@index
Displays users table with modal-based CRUD


๐Ÿšง Future Enhancements

  • ๐Ÿ” Authentication with token-based login (JWT / OAuth)
  • โš  Server-side validation and error handling
  • ๐Ÿ“„ Pagination and search
  • ๐Ÿ“ File upload via API (image/avatar)
  • โš› Migration to AJAX (Single Page API Client)
  • ๐Ÿงฑ Refactor toward mini PHP MVC Framework

๐Ÿ“„ License

MIT License. Free to use, modify, and improve.


๐Ÿ‘ค Author

Muhammad Farras Subahan
Web Developer & API Learner
๐Ÿš€ Focus: Native PHP, API Architecture, Laravel, Next.js


"Frameworks come later. Foundation always comes first."