Basic PHP Restful Web Services Implementation(Web API)

API in Programming Overview:

          Application program interface (API) is a set of Instructions, protocol, or implementation that is created to be accessible in client side application. It is also used for securely transmitting of data when it comes to the database application. It Controls data flow and limit client side application to have a full control of the main data. A good API can secure database from being vulnerable to hacking.

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 actual database connection from a database, we are going to use an Array of data to represent our database and process data in Web API, and pass data to our client application which I’m going to use JQuery in receiving data from the API.

 

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 www.000webhost.com for my hosting . Click Here for free web hosting

I uploaded myWebAPI.php in 000WebHost and consume it using xampp Localhost in my local Computer.

Database.php

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

 

Array return data
• 1 = > Asp.Net
• 2 => PHP
• 3=> C#

 

If $find value is equal to 1 the return should be Asp.Net

 

myWebAPI.php

Consume.php

Note:
Common problem encountered in JQuery is the Cross-origin Resource Sharing (CORS) that is block in most of the hosting sites. As a work around we used JQuery JSONP datatype.

 

Ajax URL:

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

Leave a Reply

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