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.
- Autoloading: Automatically loads classes from the
src/Controller
andsrc/Database
directories usingspl_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.
- PHP 7.4+
- MySQL 5.7+
- Apache Web Server (XAMPP or WAMPP recommended along with php)
-
Clone the Repository
git clone https://github.com/BekaMan95/pure_php_rest_api.git cd pure_php_rest_api
-
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';
-
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; } } });
-
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.
-
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}
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
}