PHP Rest API Example

PHP Rest API Example
PHP Rest API Example

Hi friends, if you are wondering on how to create a PHP Rest API or perform a CRUD operation using PHP Rest API then you have come to the right place. Creation of rest API is very easy and I am going to explain it in a step-by-step process. In this example, you will learn the CRUD operation with the help of REST(Representational state transfer) API.

In a simple way, you will get the HTTP request from any kind of application or via POSTMAN and perform some action based on the type of request and return the response against that request.

Also read, Razorpay payment gateway integration in PHP

Steps to Create PHP REST API

Step 1:- Establish the database connection as shown below. (dbconnect.php)

<?php
$servername='localhost';
$username="root";
$password="";
try
{
	$con=new PDO("mysql:host=$servername;dbname=myproject_db",$username,$password);
	$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	//echo 'connected';
}
catch(PDOException $e)
{
	echo '<br>'.$e->getMessage();
}
	
?>

Step 2:- Create a table in your MySQL database as shown below

DDL information of the table

CREATE TABLE employee (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
age int(20) NOT NULL,
department varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 3:- Create a folder name ‘restapi’ inside the root directory of your local server i.e. www.

Step 4:- Now, create a file ‘restapicreate.php’ under the restapi folder to insert the data inside the table of your MySQL database from the POSTMAN.

restapicreate.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$data = json_decode(file_get_contents("php://input"));
include('dbconnect.php');
//print_r($data);
 $empname = $data->emp_name;
 $age = $data->age;
 $department = $data->department;
// //echo $brokercode;
 $sql = "INSERT INTO employee(name,age,department) values('$empname','$age','$department')";
 $stmt = $con->prepare($sql);
 $stmt->execute();
 if($stmt->rowCount()>0)
 {
    $array = array('status'=>'success','msg'=>'Added Successfully');
    echo json_encode([$array]); 
 }
  else{
    $array = array('status'=>'success','msg'=>'Failed to Add');
    echo json_encode([$array]);  
  }
?>

The request and response from the POSTMAN are as shown below

PHP Rest API Example

Step 5:- Now, create a file ‘restapiread.php’ under the restapi folder to get the data from the table in your database and display it in the POSTMAN view.

restapiread.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$data = json_decode(file_get_contents("php://input"));
include('dbconnect.php');

 $sql = "SELECT * from employee ORDER BY id DESC";
 $stmt = $con->prepare($sql);
 $stmt->execute();
 $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
 if($stmt->rowCount()>0)
 {
    $array = array('status'=>'success','msg'=>$data);
    echo json_encode([$array]); 
 }
  else{
    $array = array('status'=>'success','msg'=>'Data Not Available');
    echo json_encode([$array]);  
  }
?>

Request and Response from POSTMAN as shown below

PHP Rest API Example

Step 6:- Now, create a file ‘restapiupdate.php’ to update the data in the table of your MySQL database from the POSTMAN

restapiupdate.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$data = json_decode(file_get_contents("php://input"));
include('dbconnect.php');
//print_r($data);
 $empname = $data->emp_name;
 $age = $data->age;
 $department = $data->department;
// //echo $brokercode;
 $sql = "update employee set name='$empname',age='$age',department='$department' where name='$empname'";
 $stmt = $con->prepare($sql);
 $stmt->execute();
 if($stmt->rowCount()>0)
 {
    $array = array('status'=>'success','msg'=>'Updated Successfully');
    echo json_encode([$array]); 
 }
  else{
    $array = array('status'=>'success','msg'=>'Failed to Add');
    echo json_encode([$array]);  
  }
?>

Request data from the POSTMAN with Response:

PHP Rest API Example

Step 7:- Now, create a file ‘restapidelete.php’ under the restapi folder to delete the data from the table of your MySQL database from the POSTMAN.

restapidelete.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$data = json_decode(file_get_contents("php://input"));
include('dbconnect.php');
//print_r()

 $sql = "select * from employee where id='$data->id'";
 $stmt = $con->prepare($sql);
 $stmt->execute();
 $row = $stmt->fetch(PDO::FETCH_ASSOC);
 if($row==""){
  $array = array('status'=>'success','msg'=>'ID not available');
    echo json_encode([$array]); 
 }
 else{
   $sql1 = "delete from employee where id='$data->id'";
   $stmt1 = $con->prepare($sql1);
   $stmt1->execute();
   //$info = "SELECT * from employee where id='$data->id'";
   $array = array('status'=>'success','msg'=>'Deleted Successfully');
   echo json_encode([$array]); 
 }
?>

Request and Response from POSTMAN as shown below

PHP Rest API Example

Explanation:-

header("Access-Control-Allow-Origin: *")
–>This header defines whether the response from the server should be shared with a request that comes from the given origin or URL from any application or POSTMAN.

header("Content-Type: application/json")
–>This header is used to return the response from the server in JSON format.

header("Access-Control-Allow-Methods: POST")
–> This header is used to receive the request data via the POST method.

header("Access-Control-Max-Age: 3600")
–> This header indicates how long the result from the response should appear.

$data = json_decode(file_get_contents("php://input"));

Note that in the above line file_get_contents(“php://input”) is used to get the request inputs from the POSTMAN. In addition to that json_decode() is used to wrap the request inputs and convert those inputs into objects so that inputs can be declared as PHP variables. Download POSTMAN

Also check, the best password strength checker

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.

Leave a Comment

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311