Skip to content

eventhorizn/php-simple-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build a Simple REST API in PHP

This example shows how to build a simple REST API in core PHP.

Walkthrough: https://dev.to/shahbaz17/build-a-simple-rest-api-in-php-2edl

Prerequisites

Getting Started

Configure the application

Create the database and user for the project.

I ended up doing this manually. Sql file is in source code

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

Development

Install the project dependencies and start the PHP server:

composer install
php -S localhost:8008 -t api

Your APIs

Test the API endpoints using Postman.

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

POST /post

GET /posts

GET /post/{id}

PUT /post/{id}

DELETE /post/{id}

About

Setting up a simple php api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published