CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
We have used CodeIgniter version 3.1.11 for APIs
Let’s Integrate APIs in your app
open environment.ts file in src/environment folder, here you can find two environment file, one is for debug mode and one is for production mode. you can set your credentials here for better security
Visit Website for more about CodeIgniter and installation here
Now as you can see in below image, we have set many credentials for various apis
There are 5 Steps for integrate API to your apps
Step 1 : Database
First step to integrate api is for your app is to create database in your live or local phpmyadmin, here we are configured it to live server
for this open you cPANEL and open link called MYSQL Database as shown in image below
Once you click on MYSQL Database link it will redirect you to page, where we can create/manage database,user.
Now first create databse for our APIs as shown in image below, give suitable name to your database
After successfully creating database you have to add it to database.sql file find in source code. copy the name of database as shown in image below
Now Open App_code folder and open database.sql file in Code Editor as shown in image below
And Change name to newly newly created database name as shown in image below
Next step is to create database user, for that in same MYSQL Database page scroll down and you will find MYSQL Users Section and Add New User Section
Now Create User with giving suitable username and password as shown in image below
After successfully creating user, we have to add this user to our database, in order to do that just scroll down MYSQL Database page, you can see the section called Add User To Database as shown in image below
Select Recently created database user that you have created, and click Add button
Now it will redircting you to permission page for database, now select all the permission as shown image below and click submit button
Now our database is ready to use, now go to home page of cPANEL and click on phpmyadmin and it will redircting you to the phpmyadmin console page as shown in image below
Now select database you have created and in phpmyadmin console and click on import section on top of the screen as shown in image below and try to import or drag and drop database.sql from App_code folder
Wait a minute our database will execute and create database for you as shown in image below
Step 2 : APIs Setup
Now our database is ready to use for rest APIs, ok so next step is to add connection between PHP CodeIgniter to Database, here CodeIgniter provide bridge between it, called database config, in order to do that you have to add, username/password and database name to the CodeIgniter
Now Open App_code folder and open folder called API(CodeIgniter project) as shown in image below
Open file API/application/config/database.php as shown in image below
Here we have used to different variables called debug and prod commented,
- Debug for localhost
- prod for live server
If you wanted to test locally just comment out prod variables or if you wanted to run it live just comment out debug variables
Now add username, password recently created user in MYSQL Database and database which we have created in same
Done with database connection, Yeepee!!
Step 3 : Upload APIs
Uploading APIs or Make it live on server is easy, here you can upload it to sub domain or in primary domain, we suggest you to upload it in sub domain. we will demonstrate it on sub domain.
In order to create sub domain visit cPanel Home page and find Domains Sections and click on Subdomains as shown in image below
It will redirecting you to sub domain page as shown in image below, no give suitable name to your sub domain. and give default path from system. in image below you can see your Path Will be mentioned in Document Root.
After your Subdomain successfully created, visit File Manager from cPanel Home page as shown in image below
As You can see from page your subdomain folder will be created. in our demonstrate example our folder will called as grocery-api.initstore.net as shown in image below
Now open that folder and upload API folder as shown in image below, you can find upload button on top toolbar
After uploading API zip file, extract it make it root like this. in image below
Yeepee API Uploading is done!! Now verify your API by visit subdomain in our demonstrate case we have this url : http://grocery-api.initstore.net/
Once you see message like in image below then everything is fine. otherwise there will be issue on database connections like username,password,database
Step 4 : Configuration
After successfully uploading API, there few more things too be setup before proceeding to APP side.
Once you done uploading API with success message, first thing we have to add some configurations in API project
-
- open applications/config/config.php
Find code
“$config[‘base_url’] = ‘YOUR_BASE_URL/’;”
and change base_url to your domain name, in our demonstrate case we will change it to this
“$config[‘base_url’] = ‘YOUR_BASE_URL/’;”
Make sure your domain ends with index.php/
- Second is to add encryption_key for security purpose while using data from database Find code in same file
” $config[‘encryption_key’] = ‘YOUR_ENCRYPTION_KEY’;”
this will be your auth token while calling api from apps remember this, let me put this as
“$config[‘encryption_key’] = ‘123456’;”
Step 5 : Contact Informations
Open file in APIs application/models/Users_model.php and change information according to you as shown in image below