PHP’s RESTful Life

Gagan Chordia
3 min readMay 13, 2022

--

People find PHP outdated but that’s not true, it’s up to date and fully packed. So, I decided to share a common topic REST API development using PHP, and to tell that it still has its charm💪.

What’s PHP?

PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

Advantages of PHP

  • Easy to Learn
  • Cost-Efficient
  • Flexible
  • Easy Integration
  • Efficient Performance

What’s REST API?

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.
REST → Representational State Transfer
Unlike SOAP, REST is not constrained to XML but it can also return in JSON, YAML or any other format depending on client’s needs.

Advantages of REST API

  • Highly scalable
  • Platform Independent
  • Organizable
  • Can Serialize Data in JSON/XML formats
  • Cleaner

Key Constraints to consider about REST API

  • Client-Server: Client-Server operations when they are required to be mutually exclusive.
  • Stateless: Independent calling of APIs such that they don’t overlap each other.
  • Uniform Interface: This allows the evolution of applications and models which are coupled to the API.
  • Demand-based coding: This constraint allows for code for transmission via API for use within the application.

Frameworks of PHP for REST API Development

  • Laravel
  • Lumen
  • Silex
  • Limonade
  • Wave

Sample Code

This sample code focuses on Creating, Inserting, and Reading options to collect data.
NOTE: While running on localhost make sure to keep your server online.

config.php (To configure the database)
api-insert.php (Used to add data to a table)

I’m using Thunder Client (A VS Code Extension) to test the API. You can also use Postman.

api-fetch-all.php (To fetch every data from a table)

Conclusion

You can see that PHP isn’t that old or outdated, it’s just been deserted over the years. PHP can be used to simplify the backend as it can make a developer's job easier with self-describing lines of code✌.

--

--

Gagan Chordia
Gagan Chordia

Written by Gagan Chordia

Spring Boot | Java | Vue.js | R

Responses (2)