Welcome to My Blog API! This RESTful API is designed to help manage blog posts and user authentication using JWT (JSON Web Tokens) for secure access. Get started quickly with clear setup instructions, explore endpoints, and dive into the project structure below!
- 🚀 Technologies Used
- 🛠️ Installation
- 🧪 API Testing
- 📂 Project Structure
- 🗄️ Database
- 🤝 Contributing
- 📜 License
This API utilizes a stack of powerful technologies to ensure smooth, reliable performance:
- PHP: Backend language for robust API functionality
- Composer: Dependency management
- Firebase JWT: Secure token-based authentication
- PDO: For efficient database interactions
- Apache: Web server for seamless hosting
Set up the My Blog API in just a few steps:
-
Clone the repository:
git clone https://github.com/ahmedalsanadi/my-blog-api.git cd my-blog-api
-
Install dependencies (make sure you have Composer installed):
composer install
-
Set up the database:
-
Import
my_blog_api.sql
from thedatabase/
directory into your database:SOURCE database/my_blog_api.sql;
-
-
Configure your environment:
- Update database connection settings in
config.php
.
- Update database connection settings in
Test the API using tools like Postman or cURL. Here’s a guide to the available endpoints:
-
Register User
POST/my-blog-api/register
Request Body:{ "username": "your_username", "password": "your_password" }
-
Login User
POST/my-blog-api/login
Request Body:{ "username": "your_username", "password": "your_password" }
-
Get All Posts
GET/my-blog-api/posts
-
Get Post by ID
GET/my-blog-api/posts/{id}
-
Create Post
POST/my-blog-api/posts
Request Body:{ "title": "Post Title", "content": "Post Content" }
-
Update Post
PUT/my-blog-api/posts/{id}
Request Body:{ "title": "Updated Title", "content": "Updated Content" }
-
Delete Post
DELETE/my-blog-api/posts/{id}
Authentication: For protected endpoints, include your JWT token in the
Authorization
header as a Bearer token.
A quick overview of the directory structure for My Blog API:
my-blog-api/
├── controller/
│ ├── BlogController.php
│ └── UserController.php
├── database/
│ └── my_blog_api.sql
├── middleware/
│ └── AuthMiddleware.php
├── model/
│ ├── Blog.php
│ └── User.php
├── utils/
│ ├── RequestHelper.php
│ └── ResponseHelper.php
├── views/
│ └── response.php
├── Router.php
└── index.php
The database my_blog_api.sql
file located in the project root includes the SQL schema required to set up the necessary tables for My Blog API. Import this file into your database to get started.
To enable URL rewriting and support Authorization headers, create or update the .htaccess
file in your project directory with the following code:
RewriteEngine On
RewriteBase /my-blog-api/
# Enable Authorization header
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
# Your existing rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
# CORS headers (if needed)
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Authorization, Content-Type"
This configuration enables URL rewriting for clean URLs, allows Authorization headers to be passed correctly, and includes optional CORS headers if needed for cross-origin access.
Contributions are warmly welcomed! 🫱🏽🫲🏿 Whether it’s submitting a pull request, opening an issue, or discussing an improvement, every bit helps! 🚀
This project is licensed under the MIT License. Check out the LICENSE file for details.
🌟 Thank you for exploring My Blog API! If you have any questions, feel free to reach out.