How to create REST API in PHP?

July 13, 2014

REST (Representational State Transfer) is the standard design architecture for developing web services API. It is simple easy to understand and developing client-server relationship API. REST takes advantage of the HTTP request methods to layer itself into the existing HTTP architecture. We can handle GET, PUT, POST, DELETE operations through it.

A REST API allows our users to interact with your website from anything that can send an HTTP Request. Clients initiate requests to servers; servers process requests and return appropriate results. Requests and responses are built around the transfer of representations of resources. Requests utilize HTTP which is used to manage the state of resources. The responses can be sent as XML or JSON. Since REST uses HTTP. It can be used practically for any programming language.

Start with Simple PHP Example,

I am going to explain REST API with one example by create client – server API, where client sent HTTP request to server, server make necessary process and return appropriate result to client.

Suppose you want that to make an API which is needed username & password in server site API to fetch product lists from database and response to client. This example I am going to explain step by step.

STEP 1:

First of all we need to create table product (tbl_products) in database. For example see below,

id name price quantity image
1 Iphone 1000 100 iphone.png
2 Samsung Galaxy S5 950 50 s5.png
3 Sony Xperia Z2 Price 900 35 xperia.png
4 Nokia Lumia 1520 800 25 lumia.png
5 HTC One M8 16GB 800 25 htcone.png

SETP 2:

Create server.php file as server API and define username: demo123 and password: demo123 as contact to authenticate client request. After successful client request, fetch available product list from table product and change its result into JASON format to return response. It is a simple example but you can make complex API as your need.

First & second lines get username & password from client request and validate it. If validation is success then return product lists otherwise return error.

STEP 3:

Next I’ll create client.php as client file where from that page we request the server file to get product listing to display as per our desired.

As you can see, to use the API you’d simply send an HTTP request with username and password to get product lists. This URL would call with file_get_contents() and return data in JSON. PHP has a useful json_decode() function that takes a JSON encoded string and converts it into a PHP variable. You can then loop through your variable to display the data.

Post a Comment

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

*


  • Recent Posts

    • Secure Your CodeIgniter Application using CSRF Token

      27.10.2014
      by admin
      In this tutorial, we will learn how to make our website more secure & protect from CSRF when we are using CodeIgniter web application framework. We thanks CodeIgniter team who giving in built support Cross Site Rquest Forgery (CSRF or XSRF). To enable CSRF protection CodeIgniter Framework will automatically protect forms or AJAX calls from CSRF when […]
    • Multiple Themes in CodeIgniter like WordPress

      07.08.2014
      by admin
      Create multiple themes and select one of them from backend like wordpress is drawbacks of a framework. Similar CodeIgniter have not an option to select one theme from multiple theme concept but we can re arrange using CodeIgniter template concept. We are going to handle multiple themes in a very simple way, in this example […]
    • How to create REST API in PHP?

      13.07.2014
      by admin
      REST (Representational State Transfer) is the standard design architecture for developing web services API. It is simple easy to understand and developing client-server relationship API. REST takes advantage of the HTTP request methods to layer itself into the existing HTTP architecture. We can handle GET, PUT, POST, DELETE operations through it. A REST API allows […]
    • Basic security vulnerabilities in php code

      16.10.2013
      by admin
      Today I have informed PHP programmers of common security mistakes that can be overlooked in PHP scripts. In the beginning programmers fail to understand about the PHP security issues or how to make secure script. The wise programmer knows that the real question is how secure a site is. Here I have focus how to […]
    • Generating CAPTCHA Image Using PHP

      29.12.2009
      by admin
      The CAPTCHA is a very very useful test to prevent abuse on the websites. When you create a web form like registration, login, contact us, blog comment etc…, We are suffering day by day with unwanted email or web spam abuse. So if you use CAPTCHA on your website forms, this can help in stopping […]
Copyright © 2012-2013 Sujit Shah. .
Tags: PHP Programmer Nepal, Web Developer Nepal, Website Designer, CodeIgniter Developer, Yii Framework, Wordpres, OpenCart, Drupal, Front End Developer, Responsive Developer, Bootstrap, Foundation, Freelance PHP MySql Programmer Nepal, Expert Programmer Nepal, PHP, MySql, LAMP, Linux, Apache, SVN, JavaScript, Ajax, jQuery, HTML, HTML5, CSS, CSS3, XML, SOAP, PSD to XHTML, Responsive Web Design, Web Development, CMS, E-commerce, Classified, Job portal, Travel & tours, Penny Auction, Lowest Unique Bid Auction, Reverse Auction, Price Reveal Auction, Payment Gateway Integration, Nepal, India, Hong Kong, Australia, UK, USA, Singapore, Germany, Canada, Netherlands, New Zealand, Norway, Italy