REST API CRUD using PHP

by Vincy. Last modified on July 9th, 2022.

In this tutorial, we are going to create a RESTful web service in PHP for performing the CRUD operations. In previous tutorials, we have seen examples for MySQL CRUD and for creating a RESTful web service using PHP.

In this tutorial which is part of the REST API series, let us learn about how to provide a simple REST API for CRUD (Create, Read, Update and Delete) operations. We will not be using any framework as dependencies, the complete implementation will be using plain core PHP.

Let us see about the conventions I have used in this example. The READ and DELETE actions are executed by sending the action keyword in the URL and is parsed by the GET method.

In the REST Search tutorial,  we have used this method to send the search keyword as a query parameter in the URL to be searched. The ADD and UPDATE actions are called by sending the request data using POST method.

REST API Create and Update Implementation

CREATE and UPDATE requests are sent with the posted values. The UPDATE  request URL will contain the id of a particular row to be updated.

After receiving this request, the REST CRUD service will call the domain class to perform the database insert. The domain class will generate insert query using the posted values. The following figure shows how to POST data for the CREATE or UPDATE request.

I am using the “Advanced REST Client” Google Chrome plugin to test the REST APIs.

rest-create-new

Delete using REST API

The DELETE request URL will be same as the EDIT URL. The record ID to be deleted should be passed in the URL After delete, the API response will be like as shown below.

rest-delete-mysql-data

Domain Class used for this CRUD Example

The following code shows the Mobile domain class that contains the function for performing the database CRUD operations. These functions are called by the REST handler class based on the request sent from the client.

The domain class CRUD functions will return array response to the REST handler which is encoded as JSON format.

<?php
require_once("dbcontroller.php");
/* 
A domain Class to demonstrate RESTful web services
*/
Class Mobile {
	private $mobiles = array();
	public function getAllMobile(){
		if(isset($_GET['name'])){
			$name = $_GET['name'];
			$query = 'SELECT * FROM tbl_mobile WHERE name LIKE "%' .$name. '%"';
		} else {
			$query = 'SELECT * FROM tbl_mobile';
		}
		$dbcontroller = new DBController();
		$this->mobiles = $dbcontroller->executeSelectQuery($query);
		return $this->mobiles;
	}

	public function addMobile(){
		if(isset($_POST['name'])){
			$name = $_POST['name'];
				$model = '';
				$color = '';
			if(isset($_POST['model'])){
				$model = $_POST['model'];
			}
			if(isset($_POST['color'])){
				$color = $_POST['color'];
			}	
			$query = "insert into tbl_mobile (name,model,color) values ('" . $name ."','". $model ."','" . $color ."')";
			$dbcontroller = new DBController();
			$result = $dbcontroller->executeQuery($query);
			if($result != 0){
				$result = array('success'=>1);
				return $result;
			}
		}
	}
	
	public function deleteMobile(){
		if(isset($_GET['id'])){
			$id = $_GET['id'];
			$query = 'DELETE FROM tbl_mobile WHERE id = '.$id;
			$dbcontroller = new DBController();
			$result = $dbcontroller->executeQuery($query);
			if($result != 0){
				$result = array('success'=>1);
				return $result;
			}
		}
	}
	
	public function editMobile(){
		if(isset($_POST['name']) && isset($_GET['id'])){
			$name = $_POST['name'];
			$model = $_POST['model'];
			$color = $_POST['color'];
			$query = "UPDATE tbl_mobile SET name = '".$name."', model ='". $model ."', color = '". $color ."' WHERE id = ".$_GET['id'];
		}
		$dbcontroller = new DBController();
		$result= $dbcontroller->executeQuery($query);
		if($result != 0){
			$result = array('success'=>1);
			return $result;
		}
	}
	
}
?>

Download

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page