Skip to content

BekaMan95/php-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP REST API with Object-Oriented Programming

This project is a simple PHP-based RESTful API built with Object-Oriented Programming (OOP) principles. The API interacts with a MySQL database to perform CRUD (Create, Read, Update, Delete) operations.

Features

  • Autoloading: Automatically loads classes from the src/Controller and src/Database directories using spl_autoload_register.
  • Database Interaction: Uses PDO (PHP Database Objects) for secure interaction with the MySQL database.
  • CRUD Operations: Supports creating, reading, updating, and deleting records in the database.
  • Error Handling: Implements generic error handling class for error handling and returns JSON-formatted error responses.

Prerequisites

  • PHP 7.4+
  • MySQL 5.7+
  • Apache Web Server (XAMPP or WAMPP recommended along with php)

Installation

  1. Clone the Repository

    git clone https://github.com/BekaMan95/pure_php_rest_api.git
    cd pure_php_rest_api
  2. Configure Database

    Create a MySQL database and user, then update the config.php file with your database credentials:

    // config.php
    $dbhost = 'localhost';
    $dbname = 'your_database_name';
    $dbport = 1000;
    $dbuser = 'your_username';
    $dbpass = 'your_password';
  3. Set Up Autoloading

    If not using Composer, the autoload function is already set up in index.php:

    spl_autoload_register(function ($class) {
        $directories = [
            '/src/Controller/',
            '/src/Database/'
        ];
    
        foreach ($directories as $directory) {
            $file = __DIR__ . $directory . $class . '.php';
            if (file_exists($file)) {
                require_once $file;
                return;
            }
        }
    });
  4. Run the API

    You can run this API on a local server using Apache server or a tool like XAMPP on local machine:

    for linux

    sudo /opt/lampp/lampp start

    for windows

    cd C:\xampp
    xampp-control.exe

    Navigate to http://localhost/{project_dir}/api in your API request sending tool to interact with the API.

Usage

Endpoints

  • Create Table

    POST /api/create_table/{table_name}

    Example Payload:

    {
        "id": "id",
        "name": "string",
        "age": "int",
        "salary": "float",
        "is_married": "bool",
        "created_at": "timestamp"
    }
  • Drop Table

    POST /api/remove_table/{table_name}
  • Get Records

    GET /api/{table_name}

    Example Response:

    [
        {
            "id": 1,
            "name": "John Smith",
            "age": 30,
            "salary": 60000.00
        },
        {
            "id": 2,
            "name": "Jane Smith",
            "age": 28,
            "salary": 75000.00
        }
    ]
  • New Record

    POST /api/{table_name}

    Example Payload:

    {
        "id": 1,
        "name": "John Smith",
        "age": 30
    }
  • Get a Record

    GET /api/{table_name}/{id}

    Example Response:

    {
        "id": 1,
        "name": "John Smith",
        "age": 30,
        "salary": 60000.00
    }
  • Update Record

    PUT /api/{table_name}/{id}

    Example Payload:

    {
        "name": "John Smith",
        "age": 30
    }
  • Delete Record

    DELETE /api/{table_name}/{id}

Error Handling

The API returns a JSON response with appropriate HTTP status codes for errors:

{
    "error code": 1045,
    "message": "Description of the error",
    "file": "/path/to/the/file.php",
    "line": 42
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages