dev-master
9999999-dev https://github.com/jeevan15498/CodeIgniter-API-ControllerCodeIgniter API Controller
MIT
The Requires
- php >=5.6
by Community
by C Tech Hindi
api rest api rest codeigniter codeigniter rest codeigniter rest api
Wallogit.com
2017 © Pedro Peláez
CodeIgniter API Controller
This extension is powered by Jeevan Lal., (*1)
API Documentation, (*2)
\application\libraries\API_Controller.php\application\helpers\api_helper.php\application\config\api.phpToken Documentation, (*3)
\application\libraries\Authorization_Token.php\application\config\jwt.php\application\third_party\php-jwt\
You can install this project into your PC using composer., (*4)
The recommended way to install composer packages is:, (*5)
composer require ctechhindi/codeigniter-api:dev-master --prefer-source
Note: The library is used in CodeIgniter v3.8 and PHP 5.6.8., (*6)
http://codeigniter-api.speedtyping.in/api/user/login, (*7)
Simple API, (*8)
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);
GET
$this->_APIConfig();
API Request Method POST, GET, ..
$this->_APIConfig([
'methods' => ['POST', 'GET'],
]);
Before using the limit in API, we need to load the codeigniter database library.
You can also load the database library in the autoload config config/autoload.php file., (*9)
After database library loaded, database must be set in database config file config/database.php., (*10)
After creating and setting up a database, we need to create a table for API Limit [api_limit] in the database. like this., (*11)
CREATE TABLE `api_limit` (
`id` INT NOT NULL AUTO_INCREMENT ,
`user_id` INT NULL DEFAULT NULL ,
`uri` VARCHAR(200) NOT NULL ,
`class` VARCHAR(200) NOT NULL ,
`method` VARCHAR(200) NOT NULL ,
`ip_address` VARCHAR(50) NOT NULL ,
`time` TEXT NOT NULL , PRIMARY KEY (`id`)
) ENGINE = InnoDB;
The name of the database table of api limit is api_limit by default. Which we can change through the API configuration file [config/api.php]. like this., (*12)
/** * 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., (*13)
Thus this API can be run only 10 times in 5 minutes. It on IP address., (*14)
/**
* 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 limit time argument we can also use everyday which will follow the api limit per day. On the same API address, (*15)
/**
* 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 is X-API-K, which we can change in the API config file [config/api.php]. like this, (*16)
/** * API Key Header Name */ $config['api_key_header_name'] = 'X-API-KEY';
Use this code in your API Controller file., (*17)
/**
* 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 by databases. For this, we need to first create api_keys table in MySQL. like this, (*18)
CREATE TABLE `api_keys` (
`id` INT NOT NULL AUTO_INCREMENT ,
`api_key` VARCHAR(50) NOT NULL ,
`controller` VARCHAR(50) NOT NULL ,
`date_created` DATE NULL DEFAULT NULL ,
`date_modified` DATE NULL DEFAULT NULL , PRIMARY KEY (`id`)
) ENGINE = InnoDB;
The name of the database table of API Keys is api_keys by default. Which we can change through the API configuration file [config/api.php]. like this., (*19)
/** * API Keys Database Table Name */ $config['api_keys_table_name'] = 'api_keys';
Set API keys in api_keys database table And Use this code in your API Controller file., (*20)
/**
* 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 key
private function key() {
return 1452;
}
In response to the API, we can also add custom data to something like this., (*21)
$this->_APIConfig([
'key' => ['header'],
'data' => [ 'is_login' => false ] // custom data
]);
API Output ::, (*22)
{
"status": false,
"error": "API Key Header Required",
"is_login": false
}
This method is used to return data to api in which the response data is first and the second request status code., (*23)
/** * 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 |
\application\config\api_keys.php
// 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 on GitHub., (*24)
CodeIgniter API Controller is licensed under MIT, (*25)
CodeIgniter API Controller
MIT
api rest api rest codeigniter codeigniter rest codeigniter rest api