This example shows how to build a simple REST API in core PHP.
Please read https://dev.to/shahbaz17/build-a-simple-rest-api-in-php-2edl to learn more about REST API.
Clone this project with the following commands:
git clone https://github.com/shahbaz17/php-rest-api.git
cd php-rest-api
Create the database and user for the project.
mysql -u root -p
CREATE DATABASE blog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'rest_api_user'@'localhost' identified by 'rest_api_password';
GRANT ALL on blog.* to 'rest_api_user'@'localhost';
quit
Create the post
table.
mysql -u rest_api_user -p;
// Enter your password
use blog;
CREATE TABLE `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`author` varchar(255),
`author_picture` varchar(255),
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
Copy .env.example
to .env
file and enter your database deatils.
cp .env.example .env
Install the project dependencies and start the PHP server:
composer install
php -S localhost:8000 -t api
API | CRUD | Description |
---|---|---|
GET /posts | READ | Get all the Posts from post table |
GET /post/{id} | READ | Get a single Post from post table |
POST /post | CREATE | Create a Post and insert into post table |
PUT /post/{id} | UPDATE | Update the Post in post table |
DELETE /post/{id} | DELETE | Delete a Post from post table |
Test the API endpoints using Postman.
See License