A simple PHP-based RESTful API for an e-commerce platform with JWT authentication.
- User authentication with JWT tokens
- Product listing and details
- Protected checkout endpoint
- MySQL database integration
- CORS support for frontend applications
- Environment-based configuration (development/production)
- PHP 7.4 or higher
- Composer
- MySQL database (via DBngin on port 3307 for development)
-
Clone the repository:
git clone <repository-url>
-
Install PHP dependencies:
composer install
-
For development:
- Start DBngin on port 3307
- Set up the database:
php setup-database.php
- Start the development server:
php -S localhost:8080
-
Set the
DATABASE_URLenvironment variable to your production database URL:DATABASE_URL="mysql://root:vZfoFPPbhNIAuwhozsqbpiaGXsxxSUBG@shortline.proxy.rlwy.net:22824/railway" -
Run the production deployment script:
php deploy-production.php
-
Deploy your application to your production server
- Create a new Railway project
- Add the MySQL database URL as an environment variable in Railway:
DATABASE_URL="mysql://root:vZfoFPPbhNIAuwhozsqbpiaGXsxxSUBG@shortline.proxy.rlwy.net:22824/railway" - Deploy your code to Railway
- Run the database setup command in the Railway console:
php deploy-production.php
POST /api/login- User login to obtain JWT token
GET /api/products- Get all productsGET /api/products/{id}- Get product by ID
POST /api/checkout- Process checkout (requires authentication)
When making a POST request to /api/checkout, the body should be in the following format:
{
"items": [
{
"product_id": 1,
"quantity": 1
},
{
"product_id": 2,
"quantity": 2
}
]
}Each item in the items array should have:
product_id: The ID of the product to purchasequantity: The quantity of that product
This project includes a Postman collection for easy API testing:
- Import
Ecommerce_API.postman_collection.jsoninto Postman - Import
Ecommerce_API.postman_environment.jsonas environment - Follow the instructions in
POSTMAN_INSTRUCTIONS.md
- Email:
john@example.com - Password:
password123
ecommerce-server/
├── config/
│ ├── database-connection.php
│ └── dotenv.php
├── controllers/
│ ├── AuthController.php
│ └── ProductController.php
├── middleware/
│ └── AuthMiddleware.php
├── models/
│ ├── Product.php
│ └── User.php
├── routes/
│ └── api-endpoints.php
├── setup-database.php
├── deploy-production.php
├── test-api.php
├── check-users.php
├── test-railway-connection.php
├── Ecommerce_API.postman_collection.json
├── Ecommerce_API.postman_environment.json
├── POSTMAN_INSTRUCTIONS.md
├── README.md
├── index.php
├── .env
├── composer.json
└── composer.lock
The application supports environment-based configuration:
- Development: Uses DBngin on localhost:3307
- Production: Uses DATABASE_URL environment variable
To set up for production, add your database connection string to the .env file or set it as an environment variable.
- For development: Ensure DBngin is running on port 3307
- For production: Verify the DATABASE_URL environment variable is set correctly
The project already includes function_exists() checks to prevent this issue.
Run php setup-database.php to ensure the sample user exists with the correct credentials.