How to Build a Simple Restful API in PHP

API in Programming Overview:

          Application program interface (API) is a set of instructions, protocols, or implementation created to be accessible in a client-side application. It is also used for securely transmitting data when it comes to the database application. It Controls data flow and limits client-side applications to have full control of the main data. A good API can secure the database from being vulnerable to hacking. In this article, we will create a simple Restful API in PHP.

Basic API Concept Diagram sample:

Restful Web Services

API Concept is to transmit data from a Database and used it in a Client side application like Websites or windows application through our Web API.

Instead of using direct database connection from a server, we are going to use an Array of data to represent our database and return the data using REST API, And get the data using Ajax/JQUERY.

Coding Diagram:

  • Database.php => Contain Array of Data to represent our Database
  • myWebAPI.php => Represent as Web API
  • Consume.php => Represent as our Client side application that consumes our API

I use hostinger.com for my hosting . Click Here for Hostinger plans

I uploaded myWebAPI.php in Hostinger.com and consume it using xampp in my localhost.

Simple Restful API in PHP

  1. Database.php

This code is to represent our database

<?php
function get_data ($find){
         $data =array("1"=>"Asp.Net",
                                  "2"=>"PHP",
                                 "3"=>"C#");
 
 foreach($data as $data =>$Prog_name)
    {
              if($data==$find)
   {
 return $Prog_name;
                 break;
        }
    }
}
?>
Note:

The concept was to get the value that represent each number.

$find => Parameter need to be pass to select data from array

This are the numbers and value in our example.

Array sample data
IndexValue
1Asp.Net
2PHP
3C#
If $find value is equal to 1 the return should be Asp.Net
2. myWebAPI.php

This code is the Rest API that connects the client side to the sample database.php. Which stands for the data in the database.

 <?php
 header("Content-Type:application/javascript");

include("Database.php");

if(!empty($_GET['number'])){

$number=$_GET['number'];

$Lname=get_data($number);

if(empty($Lname))
 {
 deliver_response(“Data not found”);
 }else
 {
 deliver_response($Lname);
 }
 }

function deliver_response($data)
 {
 header("HTTP/1.1 $status $status_message");
 $response['data']=$data;
 $json_response=json_encode($response);
 echo $_GET['jsonCallback'].'('.$ json_response.')';
 }
 ?>

Note: Database.php and myWebAPI.php should be uploaded in the same directory or Folder.

3. Consume.php

And now the last part was to test the API that we created. This code is to execute GET Request to the MyWebAPI.php.

<html>
 <head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <script>
 $(function(){
 $.getJSON("https://websites.com/API/myWebApi.php?number=r&jsonCallback=?",
 function(data){
 for(Lname in data)
 {
 var name = data[Lname];
 alert(name);
 }
 }
 )
 });
 </script>
 </head>
 <body>
 </body>
 </html>
Note:

A common problem encountered in the AJAX call is the Cross-origin Resource Sharing (CORS). One of the solutions is to use JSONP datatype.

Ajax URL:

  • jsonCallback => the callback we specify in myWebAPI.php ($_GET[‘jsonCallback’].'(‘.$ json_response.’)’;)
  • number => the parameter we declare in myWebAPI.php (($_GET[‘number’])))

 I hope that this will help you visualize the concept and give you idea on how to create a rest API in PHP.

Although there are a lot of frameworks for this stuff, like Laravel, Slim framework, and many more. For reference, I found this link on google. Lightweight Rest API framework. Happy coding!! See you!

Visit my Blog latest post for more.

Best way to make passive income.