How to create RESTful API / Web Service with Slim, PHP & MySQL

How to create RESTful API / Web Service with Slim, PHP & MySQL

For this article we will be using the slim PHP micro framework to implement the RESTful API. Slim was chosen because it is very light weight, clean, supports all the HTTP methods, and also provides support for middleware layers. Along with slim you will need a web server (apache or nginx), PHP, and MySQL installed before proceeding. Once you’ve gotten all the necessary files you can begin.

This tutorial gives you complete examples of creating full Restful API using multiple HTTP methods like GET,POSTPUT and DELETE. You would get the output in JSON and create a user data for all options, download code available.

HTTP methods:

GET: Used to retrieve and search data.

POST: Used to insert data.

PUT: Used to update data.

DELETE: Used to delete data.

Add below .htaccess file in your api folder http://localhost/api

Database design and table:
database name => rest
table name => restAPI
column names => id, name, email, ip, date
Database file run in your MySQL to create database and add data in table.

Database configuration

Edit database name, user and password as per your configuration

Used PDO connection you can use as per your ease and I have added this function in same methods file you can also manage them as per your projects.

Implement API

We have created 6 API methods

  1. getUsers
  2. getUser
  3. findByName
  4. addUser
  5. updateUser
  6. deleteUser

Define HTTP routes:

These all routes call individual function as defined above and in the last $app->run(); used to run Slim application.

let’s see functions:

1. getUsers: $app->get(‘/users’, ‘getUsers’);

This function simply return all users information as you can see in this query, to call this API use this URL http://localhost/api/users this is it for your first API using get route.

2. getUser: $app->get(‘/users/:id’,    ‘getUser’); In this route we are sending id.

This function check record of given id and return if found any thing,  to call this API use this URL http://localhost/api/users/1.

3. findByName: $app->get(‘/users/search/:query’, ‘findByName’); Route is used to search record with extra parameter and a search query with simple get method.

This function search in database for your given query, to call this API use this URL http://localhost/api/users/search/f.

4. addUser: $app->post(‘/users’, ‘addUser’); API used to add new record and accept post.

This API accept post request and insert submitted data in your database as we received parameters in starting of that function. To call this API I have used cURL, you can use jQuery or any other technique.

This form will be submitted to your action file and there is a curl and that curl will post data as I have used a hidden input of _METHOD with POST value this is because some times your cURL don’t Post data to API (faced this so used this for post as well) and Slim give us this functionality to method override and as our modern browsers do not have native support to PUT and delete so we have to use method overriding like below.

cURL function to post data:

5. updateUser: $app->put(‘/users/:id’, ‘updateUser’); This route accept put HTTP method.

This API function update your data by id, to call this API we need to again use cURL and HTML form.

6. deleteUser: $app->delete(‘/users/:id’,    ‘deleteUser’); Route used to delete specific ID.

This API function accept HTTP delete request and to send HTTP delete method we have to used method overriding using our hidden field named _METHOD and value will be DELETE form and cURL code given below.

That’s all for our one of the biggest tutorial for RESTful API, I hope you like this tutorial and please don’t forget to give us your feedback and any issue you have faced in this tutorial please do comment we try our level best to solve your problems.

Author Surjit Sidhu

Surjit SidhuSurjit is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling.

Leave a Reply

Be the First to Comment!