Dear Reader, let us we know that “How to create simple REST API in PHP with Live Demo“. So in this Tutorial, We Learn Step By Step How to create simple REST API in PHP Process.
API stands for “Application Programming Interface”. It is a set of rules that allows one piece of the software application to talk to another. Those “rules” can include creating, read, update and delete operations.
REST stands for “REpresentational State Transfer”. It is a concept or architecture for managing information over the internet. REST concepts are referred to as resources. A representation of a resource must be stateless.
First, we see Snapshots for our REST API form using which we can fetch data
And you can also see Live Demo by click on bellow Demo button
Step 1:- First of all we need to create the database like php_demo
Step 2:- Now create a table “mobiles” in a database using the below SQL code
1 2 3 4 5 6 7 8 |
CREATE TABLE `mobiles` ( `id` int(11) NOT NULL, `model_no` varchar(30) NOT NULL, `mobile_name` varchar(30) NOT NULL, `company` varchar(40) NOT NULL, `mobile_category` text NOT NULL, `price` double(16,2) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step 3:- Now dump some data for table “mobiles” using bellow SQL code
1 2 3 4 5 6 7 8 9 |
INSERT INTO `mobiles` (`id`, `model_no`, `mobile_name`, `company`, `mobile_category`, `price`) VALUES (13, 'SM-G615FZKUINS', 'Samsung Galaxy On Max (Black, ', 'Samsung', 'Smartphones', 20800.00), (14, ' SM-G955FZKGINS', 'Samsung Galaxy S8 Plus (Midnig', 'Samsung', 'Smartphones', 18300.00), (15, 'MN0X2HN/A', 'Apple iPhone 6s (Silver, 32 GB', 'Apple', 'Smartphones', 50000.00), (16, 'MQ8E2HN/A', 'Apple iPhone 8 Plus (Silver, 6', 'Apple', 'Smartphones', 60200.00), (17, ' R1 R829', 'OPPO R1 R829 (Black, 16 GB) (', 'OPPO', 'Smartphones', 19000.00), (18, 'F1', 'OPPO F1 (Gold, 16 GB) (3 GB R', 'OPPO', 'Smartphones', 15500.00), (19, 'MZB5602IN', 'Redmi 4A (Gold, 32 GB) (3 GB ', 'Xiomi', 'Smartphones', 5999.00), (20, 'MZB5653IN', 'Mi A1', 'Xiomi', 'Smartphones', 17500.00); |
Step 4:- Now we need to create a db.php file in our project in DB folder
like
C:\xampp\htdocs\php_demo/db/db.php
and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php define('DB_NAME','php_demo'); define('DB_USER','root'); define('DB_HOST','localhost'); define('DB_PASSWORD',''); $con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); if(!$con) { echo 'Database not connected'; } ?> |
Step 5:- create index.php file in our project in API folder like “C:\xampp\htdocs\php_demo\api\index.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php include('../header.php'); ?> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="../assets/css/style.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <h2 class="alert alert-warning">How to create simple REST API in PHP</h2> <form class="form-inline" action="" method="POST"> <div class="form-group"> <label for="name">Search Mobiles(Apple,Samsung, Sony, Redmi etc):</label> <input type="text" name="name" class="form-control" placeholder="Please Enter Mobile Name" required/> </div> <button type="submit" name="submit" class="btn btn-success">Find</button> </form> </div> <?php if(isset($_POST['submit'])) { $name = $_POST['name']; $url = "http://localhost/php_demo/api/read.php?name=".$name; $client = curl_init($url); curl_setopt($client,CURLOPT_RETURNTRANSFER,true); $response = curl_exec($client); $result = json_decode($response); //print_r($result); $array = json_decode(json_encode($result), True); //echo '<pre>'; print_r($array); //echo '</pre>'; } ?> |
Step 6:- Finally we need to create read.php file in our project like “C:\xampp\htdocs\php_demo\api\read.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php header("Content-Type:application/json"); include_once("../db.php"); if(!empty($_GET['name'])) { $name=$_GET['name']; $mobiles = getMobiles($name, $con); if(empty($mobiles)) { jsonResponse(200,"Product Not Found",NULL); } else { jsonResponse(200,"Product Found",$mobiles); } } else { jsonResponse(400,"Invalid Request",NULL); } function jsonResponse($status,$status_message,$data) { header("HTTP/1.1 ".$status_message); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response = json_encode($response); echo $json_response; } function getMobiles($name, $con) { $sql = "SELECT id, m.mobile_name, m.model_no, m.price, m.company,m.mobile_category FROM mobiles m WHERE m.mobile_name LIKE '%".$name."%' ORDER BY m.price DESC"; $result = mysqli_query($con, $sql) or die("database error:". mysqli_error($con)); $data = array(); while( $rows = mysqli_fetch_assoc($result) ) { $data[] = $rows; } return $data; } ?> |
Step 7:- Now run your project, so we need to type in browser localhost/your_project_name/api/
like
http://localhost/php_demo/api/ then you can see the result as in below snapshot, you can Fetch data in using REST API by type value in the form and click on Find button
Congratulations you have successfully created Simple REST API in PHP, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks.
5 Comments:
TomasSmall December 05, 2017
I have checked your site and i’ve found some duplicate content, that’s why you don’t rank high in google’s search results,
but there is a tool that can help you to create 100% unique articles, search for: Boorfe’s tips
unlimited content
Webpreparations Team December 22, 2017
Thank you soo much for appreciating and best suggestions. 🙂
rardlem August 24, 2019
Hello. And Bye.
WebPreparations Team August 25, 2019
Hello,
how we can help you??
rardlem December 09, 2019
i am from Italy hello. Can you help me translate? /rardor