Welcome Guest, Not a member yet? Register   Sign In
Most feature rich plugin for creating REST APIs at this moment
#1

Hi Community,

Please advise REST plugin for creating APIs that is latest and feature rich as of now.
For your information, APIs will be data intensive.

Please advise.
Reply
Reply
#3

(This post was last modified: 12-01-2018, 12:52 PM by skunkbad.)

(12-01-2018, 02:10 AM)zmart Wrote: Hi Community,

Please advise REST plugin for creating APIs that is latest and feature rich as of now.
For your information, APIs will be data intensive.

Please advise.

Something I worked on a couple years ago. Allows for a lot of flexibility in the way you handle REST requests in your controllers:


PHP Code:
<?php 
/**
 * REST
 * Author: Robert B Gottier
 * License: BSD 3-Clause
 *
 * The goal of this simple class is to make available the
 * necessary information and data related to REST APIs.
 *
 
 # -------------------------
 # Basic example usage 
    $REST = new REST;
    if( $REST->method == 'put' )
        $data = $REST->input_data;
 # -------------------------

 # -------------------------
 # Example allowing POST as PATCH 
 # overridden by $_POST['_method'] = 'PATCH'
    $REST = new REST([
           '_override_by_post_var' => TRUE
    ]);
    if( $REST->method == 'patch' )
        $data = $REST->input_data;
 # -------------------------

 # -------------------------
 # Example allowing POST as PUT 
 # overridden by X-HTTP-Method-Override header = 'PUT'
    $REST = new REST([
           '_override_by_header' => TRUE
    ]);
    if( $REST->method == 'put' )
        $data = $REST->input_data;
 # -------------------------

 *
 * Note: no attempt has been made to validate or sanitize 
 *       the data available in $REST->input_data.
 */

class REST {

    
/**
     * The detected HTTP request method
     */
    
public $method '';

    
/**
     * The detected content type
     */
    
public $content_type '';

    
/**
     * Input data from PUT and PATCH requests
     */
    
public $input_data = [];

    
/**
     * Allowed HTTP methods
     */
    
protected $_allowed_methods = ['get','delete','post','put','options','patch','head'];

    
/**
     * Overridable HTTP methods
     */
    
protected $_overridable_methods = ['post'];

    
/**
     * Replacement HTTP methods
     */
    
protected $_replacement_methods = ['put','patch'];

    
/**
     * Allow method overrides by post variable
     */
    
protected $_override_by_post_var FALSE;

    
/**
     * Allow method overrides by header
     */
    
protected $_override_by_header FALSE;

    
/**
     * The request headers
     */
    
protected $_headers = [];

    
/**
     * php://input
     */
    
protected $_php_input NULL;

    
// -----------------------------------------------------------------------

    /**
     * Class constructor
     * -----------------
     *
     * @param  array  class configuration
     */
    
public function __construct$params = [] )
    {
        
// Any key that is a REST class property can be configured.
        
foreach( $params as $key => $value )
        {
            if( 
property_exists$this$key ) )
                
$this->$key $value;
        }

        
/**
         * CLI indicates that we are doing tests through phpunit.
         * Run automatically if we are not doing tests.
         */
        
if( ! ( PHP_SAPI === 'cli' OR defined('STDIN') ) )
            
$this->run();
    }

    
// -----------------------------------------------------------------------

    /**
     * Get all headers, detect method, detect content type, and get input data.
     */
    
public function run()
    {
        
$this->_headers     $this->_get_request_headers();
        
$this->method       $this->_detect_method();
        
$this->content_type $this->_detect_content_type();
        
$this->input_data   $this->_fetch_input_data();
    }
    
    
// -----------------------------------------------------------------------

    /**
     * Get the request headers
     */
    
protected function _get_request_headers()
    {
        
// For whatever reason, getallheaders wasn't available when testing
        
if( function_exists('getallheaders') )
            return 
getallheaders();

        return 
NULL;
    }
    
    
// -----------------------------------------------------------------------

    /**
     * Detect the request method
     * -------------------------
     * A put or patch request could come from an HTML form, 
     * but it would need to use POST as the request method, 
     * so we may allow the post var "_method" to indicate that
     * we're doing a post that should be seen as a put or patch.
     *
     * We may also allow the X-HTTP-Method-Override header to 
     * change the request method to put or patch.
     *
     * If the override is not allowed, the method is not changed.
     */
    
protected function _detect_method()
    {
        
$method = isset( $_SERVER['REQUEST_METHOD'] )
            ? 
strtolower$_SERVER['REQUEST_METHOD'] )
            : 
NULL;

        
// Method override ?
        
if( ! is_null$method ) && in_array$method$this->_overridable_methods ) )
        {
            
// Method override by header
            
if( 
                isset( 
$this->_headers['X-HTTP-Method-Override'] ) && 
                
in_arraystrtolower$this->_headers['X-HTTP-Method-Override'] ), $this->_replacement_methods )
            ){
                
$method $this->_override_by_header
                    
strtolower$this->_headers['X-HTTP-Method-Override'] )
                    : 
$method;
            }

            
// Method override by POST var
            
else if( 
                isset( 
$_POST['_method'] ) && 
                
in_arraystrtolower$_POST['_method'] ), $this->_replacement_methods )
            ){
                
$method $this->_override_by_post_var
                    
strtolower$_POST['_method'] )
                    : 
$method;
            }
        }

        return 
in_array$method$this->_allowed_methods )
            ? 
$method
            
NULL;
    }
    
    
// -----------------------------------------------------------------------

    /**
     * Try to find the declared mime type for the request
     */
    
protected function _detect_content_type()
    {
        if( isset( 
$_SERVER['CONTENT_TYPE'] ) )
        {
            
$type $_SERVER['CONTENT_TYPE'];

            if( ! empty( 
$type ) )
            {
                
// Only the first part of the header is the mime type
                
return strpos$type';' ) !== FALSE 
                    
currentexplode';'$type ) ) 
                    : 
$type;
            }
        }

        return 
NULL;
    }
    
    
// -----------------------------------------------------------------------

    /**
     * For put or patch requests, the input data will be in php://input,
     * unless this is a POST disguised as a PUT or PATCH.
     */
    
protected function _fetch_input_data()
    {
        
// If there's a post array, just return it
        
if( in_array$this->method, ['put','patch'] ) && ! empty( $_POST ) )
            return 
$_POST;

        
// Otherwise, check PHP's input stream
        
if( in_array$this->method, ['put','patch'] ) )
        {
            
$input $this->_cache_php_input();

            if( ! empty( 
$input ) )
                return 
$this->_process_php_input();
        }

        return 
NULL;
    }
    
    
// -----------------------------------------------------------------------

    /**
     * Cache contents of php://input
     */
    
protected function _cache_php_input()
    {
        ! 
is_null$this->_php_input ) OR 
            
$this->_php_input file_get_contents('php://input');

        return 
$this->_php_input;
    }
    
    
// -----------------------------------------------------------------------

    /**
     * Parse, decode, unserialize, or otherwise process the php input.
     */
    
protected function _process_php_input()
    {
        switch( 
$this->content_type )
        {
            case 
'application/x-www-form-urlencoded':
                
parse_str$this->_php_input $vars );
                break;
            case 
'application/json':
                
$vars json_decode$this->_php_inputTRUE);
                break;
            case 
'application/vnd.php.serialized':
                
$vars unserialize$this->_php_input );
                break;
            default:
                
$vars $this->_php_input;
        }
        
        return 
$vars;
    }
    
    
// -----------------------------------------------------------------------

}

/* End of file REST.php */ 
And this is a little example of a controller to get started. It's using Community Auth:

PHP Code:
<?php if( ! defined('BASEPATH') ) exit('No direct script access allowed');

class 
Rest_api extends MY_Controller{

    private 
$rest NULL;
    private 
$allowed_methods = ['get','delete','post','put','patch'];
    
    public function 
__construct()
    {
        
parent::__construct();

        require_once( 
APPPATH 'libraries/REST.php' );

        
$this->rest = new REST([
            
'_allowed_methods' => $this->allowed_methods
        
]);
    }

    
// -----------------------------------------------------------------------

    /**
     * Routing remap to handle REST API endpoints.
     */
    
public function _remap$seg$params = [] )
    {
        if( 
in_array$this->rest->method$this->allowed_methods ) )
        {
            
// GET
            
if( empty( $seg ) && $this->rest->method == 'get' )
            {
                
$this->_get();
            }

            
// POST
            
else if( empty( $seg ) && $this->rest->method == 'post' )
            {
                
$this->_post();
            }

            
// GET, PUT, PATCH, DELETE
            
else if( 
                ! empty( 
$seg ) && 
                
ctype_digit$seg ) && 
                
in_array$this->rest->method, ['get','put','patch','delete'] ) )
            {
                
$meth '_' $this->rest->method;
                
$this->$meth$seg );
            }

            
// REST method and seg don't make sense
            
else
            {
                
// 400 Bad Request
                
http_response_code(400);
            }
        }

        
// INVALID REQUEST METHOD
        
else
        {
            
// 405 Method Not Allowed
            
http_response_code(405);
        }
    }

    
/**
     * GET request to URI like:
     *     http://example.com/rest_api to access list of all widgets.
     *     http://example.com/rest_api/1 to see widget #1.
     */
    
protected function _get$id )
    {
        if( 
$this->verify_role('Sudo') )
        {
            
// ...
        
}

        
// Authentication failed
        
else
        {
            
// 403 Forbidden
            
http_response_code(403);
        }
    }
    
    
// -----------------------------------------------------------------------

    /**
     * POST request to URI like:
     *     http://example.com/rest_api to create a new widget
     */
    
protected function _post()
    {
        if( 
$this->verify_role('Sudo') )
        {
            
// Successful resource creation returns a 201 Created HTTP response code
            
http_response_code(201);
        }

        
// Authentication failed
        
else
        {
            
// 403 Forbidden
            
http_response_code(403);
        }
    }
    
    
// -----------------------------------------------------------------------

    /**
     * PUT request to URI like:
     *     http://example.com/rest_api/1 to do replacement type update on widget #1.
     */
    
protected function _put$id )
    {
        if( 
$this->verify_role('Sudo') )
        {
            
$_PUT $this->rest->input_data;

            
// ...
        
}

        
// Authentication failed
        
else
        {
            
// 403 Forbidden
            
http_response_code(403);
        }
    }
    
    
// -----------------------------------------------------------------------

    /**
     * PATCH request to URI like:
     *     http://example.com/rest_api/1 to do partial update on widget #1.
     */
    
protected function _patch$id )
    {
        if( 
$this->verify_role('Sudo') )
        {
            
$_PATCH $this->rest->input_data;

            
// ...
        
}

        
// Authentication failed
        
else
        {
            
// 403 Forbidden
            
http_response_code(403);
        }
    }
    
    
// -----------------------------------------------------------------------

    /**
     * DELETE request to URI like:
     *     http://example.com/rest_api/1 to delete widget #1.
     */
    
protected function _delete$id )
    {
        if( 
$this->verify_role('Sudo') )
        {
            
// 204 No Content
            
http_response_code(204);
        }

        
// Authentication failed
        
else
        {
            
// 403 Forbidden
            
http_response_code(403);
        }
    }
    
    
// -----------------------------------------------------------------------
}

/* End of file rest_api.php */
/* Location: /application/controllers/rest_api.php */ 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB