PHP Rest API part 1

By the end of this tutorial, you should be able to create your own REST API from scratch using PHP and SLIM framework. You should also be able to know what a REST API is, the difference between a REST and SOAP API and how you can serve several clients with the same data using the same api endpoint. Be it an android app, a web dashboard, an iPhone app, windows app e.t.c.

Before diving into coding, we are going to discuss several aspects of REST and PHP frameworks you can use to create a REST api.

  1. REST – Representational State Transfer
  2. SOAP – Simple Object Access Protocol
  3. API – Application Programming Interface
  4. HTTP Methods – There are many http methods you can use but for simplicity, we are going to talk about only four of them: PUT, DELETE, GET, POST – Which I prefer to call them CRUD Methods.
  • PUT – Update a resource
  • POST – Create
  • GET – Read/ Retrieve
  • DELETE – Delete a resource

Difference between REST and SOAP

  1. Rest is stateless while SOAP is statefull – A client request should fully provide all the state information rather than depending on sessions set on the server.
  2. Rest is an architectural design pattern while SOAP is a protocol.
  3. Rest can serve data in different formats e.g. JSON, XML while SOAP mostly is XML.
  4. Rest is simpler to implement as compared to SOAP.

Some of the PHP frameworks created with an api structure in mind (List is long but I prefer these three)

  1. Slim – (My favorite :D )  - http://www.slimframework.com/
  2. Lumen – from the creators of laravel - http://lumen.laravel.com/
  3. Phalcon - https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

That’s enough of REST theory . Now lets move on to real stuff/ where you can be able to see results through writing code. We won’t be going to discuss on how to setup php webserver in your machine so for your own sake please Google and do the necessary.

In our rest api series, we will be creating an api for a movie store which should be able to to the following:

  1. List of all movies in the store
  2. List top rated movies
  3. Top recommended movies by users
  4. User can suggest a movie
  5. User can login/ register
  6. User can submit a review on a certain movie

Stay tuned for the part 2 ...

Leave a Reply

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