PHP Restful API Framework SLIM to Create REST API – 1

Hello friends, today we will see how we can use PHP Restful API Framework SLIM to create a REST API for your android application. A REST API is needed to establish the communication between the app and the server. If we talk about android then we cannot use SQLite only to create our database. We need to create the database in our server. So the communication between our database which is residing in server and our app will be done through REST API.

In this PHP Restful API Framework tutorial we will use SLIM framework to create our REST API.


This post covers the tutorial with SLIM2, but now the framework is updated to SLIM3 and we have some changes. For the updated tutorial you can watch this Play List that covers every detail of building API with SLIM.

Don’t get confused with the title, the First 11 video on this list covers SLIM only, then if you want you can learn implementing the APIs in android as well. 


To go ahead in this PHP Restful API Framework Tutorial you must have these prerquisites.

  1. Basic Knowledge of PHP
  2. Basic Knowledge of MySQL Queries
  3. PHP Storm IDE (Though you can use any other IDE but I personally love PHP Storm)
  4. WAMP / XAMPP Server (I will be using wamp server)
  5. REST Client for Firefox or Chrome for Testing.

What is REST API?

When a web service use REST Architecture we call it a REST API. REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol, and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications.

PHP Restful API Framework SLIM

In this tutorial I will be using SLIM Framework. There are more frameworks available. But I am very comfortable using it. SLIM framework is easy to use and learn. It is also very light weight and supports all kind of requests. So the first thing you need to do is Download SLIM Framework from Here.

What I will be creating in this PHP Restful API Framework Tutorial?

In this tutorial I will create a simple Android App. The app will contain two modules.

1. Student’s Module

The student will login to the app and he/she can see whether his/her assignment is submitted successfully or not. Student can see how many assignment he/she has done so far and what assignment is to be done.

2. Teacher’s Module

Teacher will login to the app and will set an assignment for a particular student. Teacher can change the assignment status to completed if the assignment is completed.

Database Design

For this app the first thing we need is our database. So below you can see the database I have designed.

Database Design
Database Design

You can see our database consist of 3 tables. This is only an abstract model now we need to create the actual database.

Creating Database

  • Go to localhost/phpmyadmin (I am using wamp server, make sure your server is online).
  • Create a new database here (I created db_studentapp).

create database

  • Now click on SQL and run the following batch query.

  • After running the above query you will see your tables are created successfully.


  • We have successfully created the database. Keep reading to create the REST API.

Basics of REST API Design

As we have designed our database now we have to create our REST API. Before starting our REST API PHP Project we should know the basics of REST API.

HTTP Methods

A REST API should use the common HTTP methods like GET, POST, PUT, DELETE. These method are used according to the requirement.

GETTo Retrieve Values from Database
POSTTo Insert new Values to Database
PUTTo Update Existing Values in the Database
DELETETo Delete Values from Database

HTTP Status Codes

Http Status Codes tells the client about the response. Like if the code is 200 that means the response is ok. The code is 401 then it is an unauthorized request. Below I have mentioned some the the request codes  for a full list of http request codes you can visit this link.

304Not Modified
400Bad Request
404Not Found
422Unprocessable Entity
500Internal Server Error

URL Structure

While creating REST API we should create good URLs. The URLs should be understandable and well formed. Every URL must be identified uniquely. For security we use API Key with the request. We should not put the API Key in the URL instead we should put the API Key in the request header. For example you can see the following URLs


The above URL will give all the assignments of the student having id 1.


The above URL will create a new assignment.


We use an API Key to restrict the access to our API. In this tutorial we will identify each faculty and student with their API Key. As this post is for beginners I am trying to simplify it as much as I can.

Creating Our REST API

Creating PHP Project using PHP Storm

  • I am using PHP Storm as I love this IDE. You can use other IDEs as well. You can also use a normal text editor.
  • First you need to create a PHP Project. I am using wamp server so in my case I should create my project at C:\wamp\www\<Project_folder>.
  • I just created a project at C:\wamp\www\StudentApp. You have to create it in the root directory of your server like it will be htdocs folder in case of xampp and www in case of wamp.

Creating Directories

  • Inside the project you need to create three more folders/directories 1. include, 2. libs, 3. v1.
    In include folder we will keep all the helper classes, in libs folder we will keep all the 3rd party libararies and in v1 folder we will store index.php that will handle all API calls.
    See the snapshot below.

    directory structure
    Directory Structure

Coding Helper Classes

  • Now the first is include. You need to create 3 php files inside include folder. Create Constants.php first and write the following code.

  • Now create DbConnect.php inside include folder. This file will help us to connect with our database. Write the following code inside this file.

  • Now the last file of include folder DbOperation.php, in this file we will write code for all the database related operations. So create DbOperation.php and write the following code.

  • In the above code I have specified methods only for student registration and student login. We have to create all the methods required here like the methods are missing for faculty registration and login. We also need to create the methods to fetch the assignment and update the assignment status. I have not written all the methods here as this tutorial is a bit lengthy I have divided it into two parts. You can find the remaining code in the second part of this PHP REST API Framework SLIM Tutorial.
  • Our helper classes are ready, now we need to handle our API calls.

Handling API Calls

  • Now come inside v1 folder and create file index.php. Here we also need to create .htaccess file to make our URL well formed. So also create .htaccess and write the following rules.

  • Now come inside index.php here we will handle all the individual API calls. Here actuallly our php restful api framework slim will be used.
  • So first create a slim instance and some required methods.

  • The above code consist of the following methods.
    echoResponse() – To display the response in json format.
    authenticateStudent() – To authenticate the student with the api key.
    verifyRequiredParams() – To verify the required parameters in the request.

Student Registration

  • The first task for us is the student registration. So we will create a unique URL for student registration. To do this you need to write the following code inside index.php.

Now we have the following method for student registration in our API.

Parametersname, username, password

The JSON Response 

Student Login

  • After registration the login part comes. So to create login we will again use post method.

Now we have the following method for student registration in our API.

Parametersusername, password

The JSON Response 

Testing Our API

  • To test the API you need a rest client. I am using Rest Easy add on for mozilla firefox. There are many other add ons available you can use any one of them.
  • To test just make a call to the URL using rest client.

rest easy


So I am wrapping up this tutorial here. As this PHP Restful API Framework Tutorial is a bit lengthy. We still need to create many other things in order to make the android application as per the requirement we discussed. So go to the below link to check the next part of this tutorial

PHP Rest API Framework SLIM to Create REST API – 2

And then  after this part of tutorial we will create an Android Application with our RESTful API. If you want to download the source code go to part 2 and you can download the whole source code.

And support us by sharing this PHP Restful API Framework Tutorial among your friends. Thank You 🙂

45 thoughts on “PHP Restful API Framework SLIM to Create REST API – 1”

  1. Hi
    Thanks for this nice tutorial. I am learning lot about REST
    Pls Belal, do not forget to include the function of keeping the session “active” until client “logout”. I am really very happy about this tutorial. Also will be nice to see how do you manage with syncronization with sqlite and remote sql, hope the syncronitation can be in groups once every certain time or manually in order not to have the telephone too busy.

    Awaititng you come soon with more.
    Thanks and best regards

  2. Excelent !!! Thanks for this Tutorial

    I hope this tutorial will help me in my projects..
    Waiting for 2nd Part

    When do you think 2nd part will be available ?


  3. Thanks, I will be waiting for it.. Could you post all methods for faculty registration and login.
    As i am learning and i´m beginner in too deficult for me to craete all methods by my own.
    I wolud like to have all methods and then compare mathods for students and faculty inorder to learn more..



  4. Hi.
    You have most of your articles with hosting done on and I tried implementing this one on hostinger. But there is one error I have encountered several times. I can’t access the MySql database. I implemented my own custom code and I always get the error in registering a user. So I narrow down the possibilities of where the error could have occurred and it seems I can’t access the DB. The credentials are correct and when I wasn’t implementing REST architecture, it was working. I had encountered the same error a month ago when I tried REST api myself.
    The code can be found here:


  5. Hi, I’m testing PHP codes Restful API to Create Framework SLIM REST API – 1. I have a hosting service payment. I Did the tests with Easy REST and I received an error 500.
    I did the following:
    1- Create the tables in my phpMyAdmin.
    2- With Dreamweaver I uploaded the StudentApp folder and all subfolders to the root directory
    3- Modified the constants.php file with data from my server (DB_USERNAME, DB_PASSWORD, etc )
    4- I Requested remote access permission to server support

    Could you help me in solving this problem..


  6. I’m testing the api with REST EASY and I am getting the code 200 and the error message that tells me syntax error in line 6 of index.php

    Line 6 is:

    $app->post(‘/createstudent’, function () use ($app) {

    My PHP Version is 5.2.17

    If I Change version to 5.5.25 or 5.4.41 I receive error 500.

    What´s wrong in the code?

  7. Hi.. ALL is OK.. Part I Works !!!!
    I´m testing all code in Part 2, and waiting for part 3 ( Android part )

    See you

    Thanks for your replay

    • Hi marcelo,

      did the first one worked for you? I have problems regarding /Slim/Slim.php file from Slim-3x version.

      I have downloaded the Slim-3x version from the link given above. But there is no Slim.php

  8. Thanks Belal.
    I will waiting for this videos

    Do you think if we can develop android project by watching the videos?
    Thanks a lot


  9. require ‘.././libs/Slim/Slim.php’

    I cant seem to find where this directory is. I have downloaded slim from the given link above. The downloaded file contains zipped folder Slim-3.x, i copied the Slim folder from the unzipped folder to the /StudentApp/libs directory. When i checked the folder. I cant find the Slim.php. Furthermore, I look for alternatives. I found this Slim-2.x version of slim. I downloaded it and found the Slim.php file. I was wondering why there is no Slim.php file from the Slim-3.x version.

  10. Hello,

    Can any one tell me how can i upload an image through post method or is there any another method to upload an image/video in slim api

    Zamin Ali Rizvi

  11. Hello,

    Thank you so much for this tutorial. I was struggling until I used your example. Nice, Straightforward, no error.
    I can’t tell if this must be optimzed or not, but it’s all I wanted.

    Thank you!

    • Heve you been able to resolve the 404 issues? I would be interested in the solutin too. I have it isntalled on WAMP and testing with SoapUI.

  12. Hello,

    This tutorial is really helpful, but i am facing a few difficulties in executing it.
    I am receiving the status code as 200 OK when i login, but the values are not retrieved from the database. Instead it is showing null.
    example: {“error”:false,”ID”:null,”display_name”:null,”user_login”:null,”apikey”:null}

  13. Hi,

    I’m new to Rest and Slim and trying to make this sample work but I keep on getting the following errors:
    Warning: require(.././libs/Slim/Slim.php): failed to open stream: No such file or directory in D:\wamp\www\StudentApp\v1\index.php on line 5
    followed by
    Fatal error: require(): Failed opening required ‘.././libs/Slim/Slim.php’ (include_path=’.;C:\php\pear’) in D:\wamp\www\StudentApp\v1\index.php on line 5

    I do have it installed in WAMP.
    What I noticed is that there is no slim.php in the folder /libs/slim nor in any other folder.
    Further more I’m wondering where the section below is to be placed in the index.php. Does it go before or after $app->run();

    //this method will create a student
    //the first parameter is the URL address that will be added at last to the root url
    //The method is post
    $app->post(‘/createstudent’, function () use ($app) {

    //Verifying the required parameters
    verifyRequiredParams(array(‘name’, ‘username’, ‘password’));

    Athor thing is that I’m wondering if the additional ./ in require ‘.././libs/Slim/Slim.php’; in dex.php is a typo or that it is to be written this way.

    I hope yu can help me.

  14. Hi

    I have copied now the full code of the 2nd part, and when I try to run it I get 404 errors. I’m testing with Soap UI

    please let me know what could be the cause.

  15. Please, can I get working code from you. I followed your steps but my server is giving a page not found error.
    Please help me out I really need to add this to my project. Thanx for great work.

  16. Warning: require(../libs/Slim/Slim.php): failed to open stream: No such file or directory in D:\xampp\htdocs\extraApi\v1\index.php on line 5

    Fatal error: require(): Failed opening required ‘../libs/Slim/Slim.php’ (include_path=’.;D:\xampp\php\PEAR’) in D:\xampp\htdocs\extraApi\v1\index.php on line 5

    how to solve this problem..
    the 5th line code is

    //including the required files
    require_once '../include/DbOperation.php';
    require '.././libs/Slim/Slim.php';


    //Creating a slim instance
    $app = new \Slim\Slim();

    please solve problem..

  17. Hii;

    Thanks for this nice tutorial. I am learning lot about REST. Now want to send the data as a parameters or want to send raw data in the web services.
    So can you tell me how I send the data as a parameter or as a raw data in my php web services using the same framework (Slim).

  18. Hi Bro.

    i created and my files structure and files as per you told actually copy paste. actually i am not able to post data whenever i tried its says fields are missings and i also try $_request method and tried to print the data but still not getting data. i also tried with your refrence project but error is same. please help bro.

  19. Hi Bro

    how do i send authentication header in volley library in android while getting all student data through android application

    Thank you
    Please help me

  20. If I add a new file say home.php then it is working. But index.php or any post method say /login giving error Not Found. Please reply, need help!

  21. HI Belal Khan Sir,

    slim api tutorials is very nice, but me suggests for core sql query likes as core php query, i am create mysql_query function using in api not likes used as bind_param function.simply used as core php query function in api.please update structure for core sql query in slim and help me creation one register api with secure parameters used in header

  22. i have download in github as you said but error like this

    Warning: require(.././libs/Slim/Slim.php): failed to open stream: No such file or directory in E:\aniket\server\htdocs\alpesh\mysite\v1\index.php on line 5

  23. Hi,
    I’m running the code and I’m receiving Error 404 – Not Found.
    I saw here people with the same problem, but no solution at all.
    Can you guys help me?


Leave a Comment