This repository contains the code for the Horizon Students API, a simple RESTful API designed to manage student information. The API provides endpoints to perform CRUD (Create, Read, Update, Delete) operations on a MySQL database table named horizonstudents. Below, you'll find information on how to use and configure the API.
I have deployed this API online, and you can test it using 👉 this link👈. You can use Postman or any API testing tool to interact with the API. (This link should work as long as the Aiven service continues provide free MySql servers.)
-
Clone the Repository:
git clone https://github.com/dev-achintha/restful-api-demo.git cd restful-api-demo -
Set Up Environment Variables: Create a
.envfile in theconfigdirectory and set the following environment variables:DB_HOST=your_database_host DB_NAME=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
-
Install Dependencies:
composer install
-
Run the API: Start a local development server:
php -S localhost:8000
The API will be accessible at
http://localhost:8000.
- GET /restful/horizonstudents: Retrieve a list of all students or filter based on specific parameters.
- POST /restful/horizonstudents: Add a new student to the database.
- PUT /restful/horizonstudents: Update an existing student's information.
- DELETE /restful/horizonstudents: Delete a student from the database.
Retrieve a list of students. You can optionally provide parameters to filter the results.
- Parameters:
index_no: Filter by student index number.first_name: Filter by student first name.last_name: Filter by student last name.city: Filter by city.district: Filter by district.province: Filter by province.email_address: Filter by email address.mobile_number: Filter by mobile number.
GET https://soc-achinthas-projects.vercel.app/restful/horizonstudents?city=Colombo&province=WesternAdd a new student to the database.
- Body:
- Provide a JSON object with student details.
POST https://soc-achinthas-projects.vercel.app/restful/horizonstudents
Content-Type: application/json
{
"first_name": "Kavindu",
"last_name": "Perera",
"city": "Colombo",
"district": "Colombo",
"province": "Western",
"email_address": "kavindu.perera@gmail.com",
"mobile_number": "0771234567"
}Update an existing student's information.
- Body:
- Provide a JSON object with the student's updated details.
- Include the
index_noof the student to be updated.
PUT https://soc-achinthas-projects.vercel.app/restful/horizonstudents
Content-Type: application/json
{
"index_no": 3,
"city": "Matara"
}Delete a student from the database.
- Body:
- Provide a JSON object with the
index_noof the student to be deleted.
- Provide a JSON object with the
DELETE https://soc-achinthas-projects.vercel.app/restful/horizonstudents
Content-Type: application/json
{
"index_no": 5
}