RESTful API in PHP | Developing REST api in core php | Memcache in php | Restful web services in core php example | RESTful API example in PHP

PHP REST API backed up with a MySQL database is a very common schematic of an Enterprise mobile application.

The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST.


HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.

db.php                                                                                                Download Code

Here in db.php we simply creates database connection.


Rest.php is the common file which will be used in every api page. In Rest.php I have created set of HTTP status codes with messages which I'm using when I'm returning the JSON response. We have a function to get the inputs and set the headers of response. I also have created a function checkAuth() to validate the API request. Currently I have used hard coded  access token you can change it according to your need. We also have a function json() which set the response array and decode it to JSON.


For saving products:

Request URL: http://localhost/core/restful-api-php/saveDetails
Method : POST
Headers: { "AccessToken" : "$2y$10$QI49JvNHcVJbldi9KMCBeeNRxatK/XVYUNgyL2HsG1HR4RiPyayDq", "Content-Type" : "application/json" }
Body: {
"name" : "Coding 4 Developers",
"productId" : 1,
"email" : "",
"age" : 27,
"company" : "Coding 4 Developers",
"address" : "Bulandshahr UP",
"mobile" : 6566564,
"height" : 5454,
"weight" : 545


For fetching products list:

Request URL: http://localhost/core/restful-api-php/getProducts
Method : POST
Headers: { "AccessToken" : "$2y$10$QI49JvNHcVJbldi9KMCBeeNRxatK/XVYUNgyL2HsG1HR4RiPyayDq" }




Share This:

2 thoughts on “RESTful API in PHP | Developing REST api in core php | Memcache in php | Restful web services in core php example | RESTful API example in PHP

  1. pradeep

    how i solve Notice: Undefined index: request in C:\xampp\htdocs\restful-api-php-master\restful-api-php-master\api.php on line 16
    {"status":0,"statusCode":404,"message":"Route not found","result":[]}

  2. Bess Hollis

    Thanks for ones marvelous posting! I definitely enjoyed reading it,
    you can be a great author. I will always bookmark your blog and
    will come back in the future. I want to encourage
    that you continue your great writing, have a nice holiday weekend!

Leave a Reply

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