In our previous tutorial, we finished the login and registration page layout that we’re going to use for this module. In this tutorial, we’ll work on our server side so we can add the database CRUD functions (Create, Read, Update, Delete) in our application. We’ll use Slim framework and XAMPP to establish a connection between our app and the server.

If you were not able to read the part 1 of this series tutorial, I recommend that you read it first before proceeding. You can read Registration App Using MySQL and PHP Part 1 here

 

What is XAMPP Server?

XAMPP is an open source cross-platform we server solution stack package developed by Apache Friends. The “X” in XAMPP stands for “operating system.” The rest of the acronym is: Apache, MySQL, PHP, and PERL. We’ll use XAMPP to establish a connection between our app and our server with the help of Slim framework to make our coding easier.

If you’re going to build an application that manages all the data on a database, then REST API will be a good option for communication between your app and server. In this module, we’ll create REST API that will enable the users to register, login their account, update their passwords and view other registered users in our database. We’ll discuss a step by step procedure on how to achieve those. Before we start you need to download the XAMPP installer here XAMPP Download Link and the Slim framework Slim Download Link.

 

1. Installing the XAMPP Server

Installing a XAMPP server in your pc is not as complicated as you think it may be. After downloading it, just run the installer and a set up wizard will appear.

 

Click next and the setup wizard will ask you what components you want to install. I recommend not to change the default selected components.

 

click next then choose a folder where you will install the XAMPP server. The click install and wait for the installation process to finish.

 

After installing the XAMPP server, open the XAMPP control panel and start the services “Apache” and “MySQL”. If you’re working with a local database make sure to start these services before establishing a connection between your app and your server.

 

2. Creating the MySQL database

Before we can start creating our REST APIs, we need first a database to work with. You can access the database admin panel by accessing http://localhost/phpmyadmin/ in your browser. Let’s start by creating a new database with a name of androidprime_registration.

Next step is to create a table named users and then create the following fields.

 

If you don’t want to create the fields manually, click the SQL tab and run the following SQL codes.

 

3. Creating the REST API

Now that our server setup is complete, we can now start creating our REST API that will add database functionality in our server. First, create a new directory with a path of C:\xampp\htdocs\android\api then create three new folders named inclue, libs and v1. You should now have a structure like in the image below. The include folder will be the place for our helper classes, the libs folder for all the third party libraries and the v1 folder for all the API calls. If you want to create a new version of your api, you simple create a new folder named v2.

Now copy the slim framework inside the libs folder and create the following php files just like in the image below.

 

Open Config.php and the following codes. This will handle all the configurations in our database such as hostname, database name, username and password.

 

Now open DbConnect.php and add the following codes. This class will handle the connection to the database.

 

Now open Constants.php and add the following codes. This class will store all the static variables used in our php programming.

 

Then open DbHandler.php and add the following codes. This class will handle all the methods and helpers that we’ll use in executing SQL queries that will fetch data from our database.

 

Finally, open index.php and copy the following codes. This will handle all the API calls we’ll be using in our application.

 

4. Testing the REST API

Before we can implement our REST API in our application, we need first to test if there are errors. We are going to use POSTMAN as our test platform. POSTMAN is a powerful GUI platform to make your API development faster & easier, from building API requests through testing, documentation and sharing. If you’re using chrome, you can get postman in the Apps Web Store. Simply click the Apps icon found in the left top corner of your chrome browser below the tabs.

After installing POSTMAN, open it and you should have a window that looks the same as I have in the image below.

 

Before we start testing our REST APIs, I want to explain first the different HTTP methods. A REST API supports most commonly used HTTP methods such as GET, POST, PUT and DELETE. Each method should be used depending on the type of operation you want to perform.

HTTP Method Type Function
GET Used when we want to fetch a resource/data
POST Used when we want to create a new resource/data
PUT Used when we want to update existing resource/data
DELETE Used when we want to delete a resource/data

 

When you call an http method, a response will send back to you. These responses can help you determine the status of your calls. Returning a status code is a good practice so that we can track what’s happening in our requests. This is the list of status codes available for REST API. You can check out list of http codes from restapitutorial.com

Status Code Definition
200 OK
201 Created
304 Not Modified
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
422 Unprocessable Entity
500 Internal Server Error

 

Now let’s test our REST APIs. These are the available APIs in our REST API structure.

URL Method Parameters Description
http://localhost/android/api/v1/verifyEmail POST email Verify if an email is already registered in the database
http://localhost/android/api/v1/register POST email, first_name, last_name, password Registers a user in the database
http://localhost/android/api/v1/login POST email, password Login a user
http://localhost/android/api/v1/updatePassword POST id,

password

Update a user’s password
http://localhost/android/api/v1/getRegisteredUsers GET Get all recent registered users in the database

 

In order to test an API in POSTMAN, we need to choose the type of the request, provide the URL of the request and add key value pairs in the body of the request if necessary then click send to get the response from our server. This is an example of a POSTMAN test.

 

If you are able to get this response, it means that you followed all the steps correctly. In our next tutorial, we are going to learn how to use our REST API in our registration app. You can download the full codes here. Download Link