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.
- REST – Representational State Transfer
- SOAP – Simple Object Access Protocol
- API – Application Programming Interface
- 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
- 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.
- Rest is an architectural design pattern while SOAP is a protocol.
- Rest can serve data in different formats e.g. JSON, XML while SOAP mostly is XML.
- 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)
- Slim – (My favorite ) - http://www.slimframework.com/
- Lumen – from the creators of laravel - http://lumen.laravel.com/
- 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:
- List of all movies in the store
- List top rated movies
- Top recommended movies by users
- User can suggest a movie
- User can login/ register
- User can submit a review on a certain movie
Stay tuned for the part 2 ...