| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This project is a simple RESTful API built in native PHP. It serves as a learning resource for those interested in understanding how to create a basic RESTful API from scratch. The API provides endpoints to manage user data, allowing users to perform CRUD (Create, Read, Update, Delete) operations.
Here are the available API endpoints:
Get All Users
GET /api/usersRetrieves a list of all users
Add New User
POST /api/usersAdds a new user to the database. Requires name, age, and job in the request body.
Get User by ID
GET /api/users/:idRetrieves user data for a specified user ID.
Update User Data
PATCH /api/users/:idUpdates user data for a specified user ID. Requires at least one of the following fields in the request body: name, age, or job.
Delete User by ID
DELETE /api/users/:idDeletes the user with the specified user ID.
The main components of the project include:
To get started with this project, follow the steps below.
git clone https://github.com/FarrelAD/Basic-PHP-RESTful-API.git
cd Basic-PHP-RESTful-APIcomposer installCreate a .env file in the project root directory to store your environment variables. This is essential for configuring database connections and other settings.
Example .env file:
DB_HOST=localhost
DB_USER=root
DB_PASS=my_password
DB_NAME=db_php_restful_apiCreate an error.log file in the logs directory to store all error logs that occur when the application runs.
(root) ├───config ├───logs │ └───error.log # This is new file to log the error ├───public ├───src │ ├───controllers │ ├───models │ └───routes └───vendor
After setting up the project, you can start the web server first. You can use Apache HTTPD, NGINX or PHP built in web server.
composer run devAfter that, you can access the API endpoints using a tool like Postman or cURL.Below are some examples of how to use the endpoints:
curl -X GET http://localhost:8000/api/userscurl -X POST http://localhost:8000/api/users -d "name=John Doe&age=30&job=Developer"curl -X GET http://localhost:8000/api/users/1
curl -X PATCH http://localhost:8000/api/users/1 -d '{"name": "Jane Doe"}'curl -X DELETE http://localhost:8000/api/users/1The API includes custom error handling to manage different scenarios, such as:
All responses are returned in JSON format, providing a consistent structure for success and error messages.
All server errors are logged to a file located at logs/error.log. This log file can be used to monitor your web application when it is live in production, helping to identify and troubleshoot issues effectively.
This project is open-source and available under the MIT License. Feel free to modify and use it as a learning resource.
| Back | FazBrowse Home | New Git URL |