Home >>PHP API

PHP Rest API

PHP Rest API Tutorial Step by Step

How to Create A Simple REST API in PHP? Step By Step Guide!

In this tutorial we will tell you how to create simple rest api in php step by step from the beginning. Before we start, you should know about what is an API and rest API.

What is an API?

Well API stands for application programming interface which defines the functions and variables. further These functions and variables use to call in order to carry out its desired task.

It is used to communicate with the database via php extensions. API is just a collection of protocols and tools for making a software application. According to me API is the simple way to interact with allocations easily.

Here below you will find step by step information and hope this will be very beneficial for beginners.

“A set of clearly defined methods of communication between various components.”

What is Rest API?

Rest API is the combination of two basic functions Rest stands for Representational State Transfer and API stand for Application Programming Interface.

well Rest API is the web architecture it uses HTTP protocol for exchanging data between two functions that means your application or system.

Basically, it is the set of functions which is used to get the data with the use of HTTP protocol like GET and POST and WWW is the best example of it because it uses rest protocol architecture to get the data.

Uses of Rest API

As you know Rest API is a program that uses HTTP request to GET, POST, PUT, DELETE, OPTION AND HEAD the Data.

Here below you can see what are the functions.

GET - it is used to transfer data from client to server in HTTP protocol using URL String

POST - it is also used to transfer data from client to server in HTTP protocol but it carries request parameter in message body which makes it more secure way

PUT - This method request is use to enclosed the entity under the supplied Request URL.

Options - It shows which technique is supportable.

HEAD - It returns the meta information.

Steps to Create Rest PHP in API

First You have to create a Database

CREATE DATABASE testapi;

Second You have to Create Table

CREATE TABLE IF NOT EXISTS `image_slider` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`description` varchar(255) NOT NULL,
`image_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

After Creating Database and Table You have to Add Data to Table

INSERT INTO `image_slider` (`id`, `title`, `description`, `image_url`) VALUES
(1, 'Slide 1', 'First of three slides', 'http://localhost/sliderimg/slide1.png'),
(2, 'Slide 2', 'Second of three slides', 'http://localhost/sliderimg/slide2.png'),
(3, 'Slide 3', 'Third of three slides', 'http://localhost/sliderimg/slide3.png');

Now Create PHP file to Output Data in Json Format

<?php
header("Content-type:application/json");
$con = mysqli_connect("127.0.0.1", "root", "", "testapi");

if(!$con){
die('Could not connect: '.mysqli_error());
}

$result = mysqli_query($con, "SELECT * FROM image_slider");

while($row = mysqli_fetch_assoc($result)){
$output[]=$row;
}

print(json_encode($output, JSON_PRETTY_PRINT));

mysqli_close($con);

?>

After Running File, you will Get the Output Data

[
{
"id": "1",
"title": "Slide 1",
"description": "First of three slides",
"image_url": "http:\/\/localhost\/sliderimg\/slide1.png"
},
{
"id": "2",
"title": "Slide 2",
"description": "Second of three slides",
"image_url": "http:\/\/localhost\/sliderimg\/slide2.png"
},
{
"id": "3",
"title": "Slide 3",
"description": "Third of three slides",
"image_url": "http:\/\/localhost\/sliderimg\/slide3.png"
}
]

Hope this article will be helpful for you and now you can see how simply you can create rest API in PHP.


PHP Rest API Index

Sr.No. Topics
1 SMS Gateway Integration in PHP
2 How to Integrate PayPal Payment Gateway in PHP
3 PayUMoney Payment Gateway Integration in PHP

No Sidebar ads