Create PHP Restful API without Any Rest Framework Dependency

This php tutorial help to create CRUD operations rest api without any rest framework, custom framework is easy to use and customizable. You can defined any structure and customized as per your need, You can define your rule for rest api framework and easily access.Its not mandatory to have a framework to build your Rest API using php. You can create rest api using core PHP code.

There are some of the awesome rest frameworks for Php is as follows,

Lets start create rest api example, I will create 'api/' folder.This folder will keep all the files which are required for this rest api tutorial.We will create rest api for employee module that will have HTTP Get,Post,Put and Delete request for fetch records from mysql,add record into MySQL, update record into mysql and delete record from mysql. The Rest details are as follows:

RouteMethodTypePosted JSONDescription
/employeesGETJSONGet all employees data
/employees/{id}GETJSONGet a single employee data
/employeesPOSTJSON{"employee_name": "Adam", "employee_age": "34", "employee_salary" : "23421"}Insert new employee record into database
/employeesPUTJSON{"employee_name": "Adam", "employee_age": "34", "employee_salary" : "23421", "id":21}Update employee record into database
/employeesDELETEJSON{"id" : 59}Delete particular employee record from database

I will use following files for this rest api tutorial,

  1. index.php : This file is entry file, This file prevent navigate folder files.
  2. connection.php : This file will use to have mysql connection string.
  3. v1/employees.php : This file will contains all rest end points and action methods.
  4. v1/.htaccess : This file use for rest end points routing purpose.

Employee table Schema

Create a 'test' MySQL database, if you have already any database then also run below sql into that database SQL query window.

MySQL Database Connection With PHP using mysqli

We will create connection.php file and add MySQL connection string to connect MySQL with php using mysqli_connect() method. We will add below code into this file.

You need to replace database name, db username, db password and hostname as per your database information.

.htaccess Rewriterule with PHP for Clean URLs

We will create .htaccess file v1/ folder and write some rule to access rest api with pretty URLs.We will add following rules,

Restful API to fetch all record from MySQL

We will create a GET type HTTP Rest Request to access all employee records from MySQL. We will use MySQL query to fetch data from employee table and send JSON data array to client as response object.

Now we will create employees.php file into v1 folder and add MySQL connection file to access database table data.

I have also use $_SERVER method to access action information, like rest request for add, edit or delete.

We will create php switch method to access right method against action rest request.

We have create GET request to fetch all employee data from mysql database, for single employee data we are passing employee id.We have define get_employees() method so we will create method like below,

mysqli_query() method fetch data from MySQL employee table ans stored as abject into 'result' variable. The json_encode() method convert arrays of data into json string.

Now access http://localhost/api/v1/employees rest api url form browser and you will get a all employees record from mysql employee table.

Restful Api to get single record from MySQL using PHP

We will create a HTTP GET type rest request to access a single employee record from MySQL database using php. its same as previous rest call just passed employee id with in function.

Now access http://localhost/api/v1/employees/1 rest api url form browser and you will get a all employees record from mysql employee table.

Rest Api to Create New Record into MySQL

We will create a new Rest Api to insert new employee record into MySQL using php. I will create POST type Rest request because We will post some JSON data to php server.

We will add new case into switch method like below,

Now we will create insert_employee() method into employees.php file.

Rest Api to Update Record into MySQL using PHP

We will create new HTTP PUT type Restful api request to update data into MySQL database.We need to pass employee id as a parameters which will use to update record into employee table.

We will add new case into switch method like below,

Now we will create update_employee() method into employees.php file.We will use employee_id which we want to update and second is updated data in json format.

We have taken employee_id as a param and json data to updated record into mysql table, if everything fine then send ‘updated message’ otherwise send error message.

Rest API to delete Record from MySQL

We will create new DELETE Type restful api request using php to remove employee record from MySQL database.We will pass employee_id as a parameters which you want to delete from employee table.
We will add new case into switch method like below,

Now we will create delete_employee() method into employees.php file.We will use employee_id which we want to delete record from employee table.

Conclusion

We have created restful api to fetch all employees,get single employee record,create new employee entry,update employee data and delete employee data from MySQL database.You can add security related option like token to restrict access of rest api from un-authorize users.