rest-apis-core-php

PHP Restful API without Any Rest Framework Dependency

REST Stands For Representational State Transfer & RESTful API is an Application Program Interface (API) uses HTTP request to GET, PUT, POST, and DELETE data from web server. Here we will learn How to implement PHP Restful API using core php code.

There are many ways to create RESTful APIs using PHP Code. For Example, you can easily create any APIs with third-party Frameworks but for that, you must have knowledge of that particular framework.

There are some awesome frameworks for creating RESTful APIs,

  • Laravel
  • Phalcon
  • Slim
  • Lumen
  • Silex

Using this tutorial we can learn How to create PHP RESTful APIs without using any framework. This tutorial contains database CRUD (Create, Read, Update, Delete) Operations. Web services that follow the RESTful principles are RESTful services.

The main intention of this tutorial is :

  • To build a PHP RESTful API / Webservice.
  • With Core PHP, without dependency on any framework.
  • Rest API Should be able to respond request in JSON, XML formats.
  • Test the RestFUL API in most-used REST client POSTMEN and APIGEE.

Let’s start to create RESTful APIs with example of PRODUCT DETAIL module and perform all database operations like,

Method Type Description
1. GET JSON To read single or multiple records.
2. POST JSON To create a new record.
3. PUT JSON To Update a record.
4. DELETE JSON To delete a record.

First, you need to create “rest_api/” folder to keep all files which are required for this PHP Restful API.

STEP 1: Create Database

Here we can create PHP Restful API with using Product example so you need to create PRODUCT_DELATIL database file in MySQL.

 

Now Create PRODUCT table with following columns: product_id, product_name, product_price, and product_qty.

STEP 2: Database Connection with PHP MySQL

Create connection.php file into “rest_api” folder and add following code into the file.

STEP 3: Create PHP REST API

Now Create new file product_details.php into “rest_api” folder for all CRUD operations of the database. This is the main APIs files and in next step, we will perform all database operations.

Copy and paste following code snippets into “product_details.php” file. Into further steps we will learn all case like GET, POST, PUT, DELETE.

STEP 4: Fetch all records from PRODUCT using GET

Use GET Type when you want to fetch all records from the server. See the following code for fetch record from MySQL Database.

STEP 5: Insert a new Record Using PHP Restful API using POST

Use POST Type when you want to insert a new record into MySQL Database. You need to pass all required parameters when you call Web Service with POST type. Use following code for that.

STEP 6: Update a Record Using Restful API using PUT

Use PUT Type when you want to update particular record into MySQL Database. You need to pass all required parameters with it’s updated value when you call Web Service with PUT type. Use following code for that.

STEP 7: Delete a Record Using Restful API using DELETE

Use DELETE Type when you wan want to delete any records from MySQL Database. You need to pass product_id to remove the perticular product.

 

Now all Step is done. Here is the full source code of “product_details.php” file.

Check Output

Now all coding related part is done. You can check your recently created web service using any Rest Client tool. For testing, you can use POSTMEN. Open any Rest Client and check the Web Service this URL:  http://localhost/rest_api/product_details.php  Select request method.

RESTful Web Service JSON Output

{
“status”: “1”,
“message”: “success”,
“result”: [
{
“product_id”: “1”,
“product_name”: “Keyboard”,
“product_price”: “350”,
“product_qty”: “15”
},
{
“product_id”: “2”,
“product_name”: “Mouse”,
“product_price”: “250”,
“product_qty”: “10”
},
{
“product_id”: “3”,
“product_name”: “Pen drive”,
“product_price”: “500”,
“product_qty”: “5”
},
{
“product_id”: “4”,
“product_name”: “Scanner”,
“product_price”: “1500”,
“product_qty”: “2”
},
{
“product_id”: “5”,
“product_name”: “32\” LED Desktop”,
“product_price”: “6500”,
“product_qty”: “7”
}
]
}

Conclusion

This is all about PHP Restful API developments without framework dependency. There many other methods are also available for APIs / Web Services development. Please feel free to comment if you have any questions, suggestions, found something wrong or want to contribute to this code. You can also learn How to implement PHP Restful Api using CodeIgniter.

Hope you like this tutorial. Cheers!!!.