PHP code example of k7brasil / codeigniter3-api

1. Go to this page and download the library: Download k7brasil/codeigniter3-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

k7brasil / codeigniter3-api example snippets


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->_APIConfig();

$this->_APIConfig([
    'methods' => ['POST', 'GET'],
]);

/**
 * API Limit database table name
 */
$config['api_limit_table_name'] = 'api_limit';

/**
 * Set API Timezone 
 */
$config['api_timezone'] = 'America/Sao_Paulo';

/**
 * 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] 
]);

/**
 * 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'] 
]);

/**
 * API Key Header Name
 */
$config['api_key_header_name'] = 'X-API-KEY';

/**
 * Use API Key without Database
 * ---------------------------------------------------------
 * @param: {string} Types
 * @param: {string} API Key
 */

$this->_APIConfig([
    'key' => ['header', 'Set API Key'],
]);

/**
 * API Keys Database Table Name 
 */
$config['api_keys_table_name'] = 'api_keys';

/**
 * 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;
}

$this->_APIConfig([
    'key' => ['header'],
    'data' => [ 'is_login' => false ] // custom data
]);

/**
 * Return API Response
 * ---------------------------------------------------------
 * @param: API Data
 * @param: Request Status Code
 */
$this->api_return(data, status_code);

// load API Keys config file
$this->load->config('api_keys');

$this->_APIConfig([
    'key' => ['post', $this->config->item('controller/api key name')],
]);