k7brasil/codeigniter3-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

k7brasil/codeigniter3-api

Composer 安装命令:

composer create-project k7brasil/codeigniter3-api

包简介

CodeIgniter API Controller

README 文档

README

This extension is powered by K Seven.

Files

API Documentation

  • \application\libraries\API_Controller.php
  • \application\helpers\api_helper.php
  • \application\config\api.php

Token Documentation

  • \application\libraries\Authorization_Token.php
  • \application\config\jwt.php
  • PHP-JWT Library \application\third_party\php-jwt\

Change Log

Installation

You can install this project into your PC using composer.

The recommended way to install composer packages is:

composer create-project k7brasil/codeigniter3-api

Postman (Test)

chrome web store ou Install In PC (Recommended)

Requirements

  1. PHP 5.4 or greater
  2. 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);

Documentation

Setup API Request Methods

  • This Function by default request method GET
$this->_APIConfig();
  • Set API Request Method POST, GET, ..
$this->_APIConfig([
    'methods' => ['POST', 'GET'],
]);

Use API Limit

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.

After database library loaded, database must be set in database config file config/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.

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.

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

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

Now we can use API Limit Method.

Thus this API can be run only 10 times in 5 minutes. It on IP 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 limit time argument we can also use everyday 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'] 
]);

Use API Key without Database

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

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

Use API Key with Database

We can also check the API key by databases. For this, we need to first create api_keys table in MySQL. like this

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.

/**
 * 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.

/**
 * API Key
 * ---------------------------------------------------------
 * @param: {string} Types
 * @param: {string} [table]
 */
$this->_APIConfig([
    // 'key' => ['header', 'table'],
    'key' => ['header'], 
]);

Use Custom Function in API Key

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

Add Custom Data in API Responses

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
}

API Return Data

This method is used to return data to api in which the response data is first and the second request status code.

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

Request Status Code List

Status Code Status Text
200 OK
401 UNAUTHORIZED
404 NOT FOUND
408 Request Timeout
400 BAD REQUEST
405 Method Not Allowed

Using the Config file in the API key

  1. Create config file \application\config\api_keys.php
  2. 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')],
]);

Reporting Issues

If you have a problem with this plugin or found any bug, please open an issue on GitHub.

License

CodeIgniter API Controller is licensed under MIT

k7brasil/codeigniter3-api 适用场景与选型建议

k7brasil/codeigniter3-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「codeigniter」 「REST API」 「CodeIgniter Rest」 「CodeIgniter Rest Api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 k7brasil/codeigniter3-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 k7brasil/codeigniter3-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 k7brasil/codeigniter3-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-01