- Notifications
You must be signed in to change notification settings - Fork47
CodeIgniter Restful API Controller - Easily build REST API with Token Authorization
License
ctechhindi/CodeIgniter-API-Controller
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This extension is powered byJeevan Lal.
\application\libraries\API_Controller.php\application\helpers\api_helper.php\application\config\api.php
\application\libraries\Authorization_Token.php\application\config\jwt.php- PHP-JWT Library
\application\third_party\php-jwt\
You can install this project into your PC usingcomposer.
The recommended way to install composer packages is:
composer require ctechhindi/codeigniter-api:dev-master --prefer-source- PHP 5.4 or greater
- CodeIgniter 3.0+
Note: The library is used in CodeIgniter v3.8 and PHP 5.6.8.
Simple API
header("Access-Control-Allow-Origin: *");// API Configuration$this->_apiConfig([/** * By Default Request Method `GET` */'methods' => ['POST'],// 'GET', 'OPTIONS'/** * Number limit, type limit, time limit (last minute) */'limit' => [5,'ip','everyday'],/** * type :: ['header', 'get', 'post'] * key :: ['table : Check Key in Database', 'key'] */'key' => ['POST','string_key' ],// type, {key}|table (by default)]);// return data$this->api_return( ['status' =>true,"result" =>"Return API Response", ],200);
- This Function by default request method
GET
$this->_APIConfig();
- Set
API RequestMethodPOST, GET, ..
$this->_APIConfig(['methods' => ['POST','GET'],]);
Before using the limit in API, we need to load the codeigniterdatabase library.You can also load the database library in the autoload configconfig/autoload.php file.
After database library loaded, database must be set in database config fileconfig/database.php.
After creating and setting up a database, we need to create a table for API Limit[api_limit] in the database. like this.
CREATETABLE `api_limit` (`id`INTNOT NULL AUTO_INCREMENT ,`user_id`INTNULL DEFAULTNULL ,`uri`VARCHAR(200)NOT NULL ,`class`VARCHAR(200)NOT NULL ,`method`VARCHAR(200)NOT NULL ,`ip_address`VARCHAR(50)NOT NULL ,`time`TEXTNOT NULL ,PRIMARY KEY (`id`)) ENGINE= InnoDB;
The name of the database table of api limit isapi_limit by default. Which we can change through the API configuration file[config/api.php]. like this.
/** * API Limit database table name */$config['api_limit_table_name'] ='api_limit';/** * Set API Timezone */$config['api_timezone'] ='Asia/Kolkata';
Now we can use API Limit Method.
Thus this API can be run only 10 times in 5 minutes. It onIP address.
/** * API Limit * ---------------------------------- * @param: {int} API limit Number * @param: {string} API limit Type (IP) * @param: {int} API limit Time [minute] */$this->_APIConfig([// number limit, type limit, time limit (last minute)'limit' => [10,'ip',5] ]);
At API limittime argument we can also useeveryday which will follow the api limit per day. On the same API address
/** * API Limit * ---------------------------------- * @param: {int} API limit Number * @param: {string} API limit Type (IP) * @param: {string} API limit [everyday] */$this->_APIConfig([// number limit, type limit, everyday'limit' => [10,'ip','everyday'] ]);
We can set the API key in the Request Header. by default, the name of the header isX-API-K, which we can change in the API config file[config/api.php]. like this
/** * API Key Header Name */$config['api_key_header_name'] ='X-API-KEY';
Use this code in your API Controller file.
/** * Use API Key without Database * --------------------------------------------------------- * @param: {string} Types * @param: {string} API Key */$this->_APIConfig(['key' => ['header','Set API Key'],]);
We can also check the API key bydatabases. For this, we need to first createapi_keys table in MySQL. like this
CREATETABLE `api_keys` (`id`INTNOT NULL AUTO_INCREMENT ,`api_key`VARCHAR(50)NOT NULL ,`controller`VARCHAR(50)NOT NULL ,`date_created`DATENULL DEFAULTNULL ,`date_modified`DATENULL DEFAULTNULL ,PRIMARY KEY (`id`)) ENGINE= InnoDB;
The name of the database table of API Keys isapi_keys by default. Which we can change through the API configuration file[config/api.php]. like this.
/** * API Keys Database Table Name */$config['api_keys_table_name'] ='api_keys';
Set API keys inapi_keys database table And Use this code in your API Controller file.
/** * API Key * --------------------------------------------------------- * @param: {string} Types * @param: {string} [table] */$this->_APIConfig([// 'key' => ['header', 'table'],'key' => ['header'], ]);
/** * API Key * --------------------------------------------------------- * @param: {string} Types * @param: [function] return api key */$this->_APIConfig(['key' => ['header',$this->key() ],]);// This is Custom function and return api keyprivatefunctionkey() {return1452;}
In response to the API, we can also add custom data to something like this.
$this->_APIConfig(['key' => ['header'],'data' => ['is_login' =>false ]// custom data]);
API Output ::
{"status":false,"error":"API Key Header Required","is_login":false}This method is used to return data to api in which the response data isfirst and thesecond request status code.
/** * Return API Response * --------------------------------------------------------- * @param: API Data * @param: Request Status Code */$this->api_return(data, status_code);
| Status Code | Status Text |
|---|---|
| 200 | OK |
| 401 | UNAUTHORIZED |
| 404 | NOT FOUND |
| 408 | Request Timeout |
| 400 | BAD REQUEST |
| 405 | Method Not Allowed |
- Create config file
\application\config\api_keys.php - Use in API Controller like this
// load API Keys config file$this->load->config('api_keys');$this->_APIConfig(['key' => ['post',$this->config->item('controller/api key name')],]);
If you have a problem with this plugin or found any bug, please open an issue onGitHub.
CodeIgniter API Controller is licensed underMIT
About
CodeIgniter Restful API Controller - Easily build REST API with Token Authorization
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.