Skip to content

anshu-krishna/PHP-API-Framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Framework

This is a simple API framework that can be used to create APIs.

Installation:

composer require anshu-krishna/api-framework

Quick Start Guide (using the provided sample code as example):

Let's create a simple API server and on the way learn how to use this framework.

See the sample directory for the full sample server code.

In this example we will assume that this server is running at https://api.sample.dev .

Files List:

codebase-root/
├─ api-funcs-base/
│  ├─ @all.php
│  ├─ @index.php
│  ├─ Ping.php
│  ├─ Example/
│  │  ├─ @all.php
│  │  ├─ @index.php
│  │  ├─ Adder.php
│  │  ├─ CamelCase/
│  │  │  ├─ @index.php
│  │  │  ├─ Hello.php
├─ public/
│  ├─ .htaccess
│  ├─ index.php

Explanation of the listed files:

  • codebase-root/ This is the root directory of the codebase. (Choose any directory).

  • codebase-root/api-funcs-base/ This is the directory where all the API definitions are stored. (Directory name does not need to be api-funcs-base. It can be anything.)

  • codebase-root/public/ This is the public directory where the server is running. (i.e. https://api.sample.dev must point to this directory.) Directory name does not need to be public. It can be anything.

    • .htaccess This file is used to rewrite the URLs to the API framework.

    • index.php This file is the entry point of the server.

Sample code for some of the files (with explanations/comments):

<?php  // file: codebase-root/public/index.php

require_once '../vendor/autoload.php';

use Krishna\API\Config;
use Krishna\API\Server;

Config::$dev_mode = true; // Set to false in production
Config::$zlib = false;    // Set to true if you want to compress the output

// See Krishna\API\Config for more options


// Initialize the server
Server::init(
	func_base_path: __DIR__ . '/../api-funcs-base',
);

// Start executing api request
Server::execute();
<?php  // file: codebase-root/api-funcs-base/Ping.php

// This API expects either no parameters or a single parameter 'msg' of type string;
// See DataValidator in the notes below to see more examples of possible signatures

use Krishna\API\Func;

// Set the signature of the function
Func::set_signature([
	'?msg' => 'string',
]);


// Set the definition of the function
Func::set_definition(function(array $data, string $funcName) {
	if(!array_key_exists('msg', $data)) {
		return 'Hello; No message received';
	}
	return 'Hello; Message received: ' . $data['msg'];
});

See the sample directory for the full sample server code.


Sample Requests and Responses:

Request:

https://api.sample.dev/ping

Response:

{
    "status": 0, // 0 = Success
    "value": "Hello; No message received",

    // Meta information; Only available in dev_mode
    "meta": {
        "exe_time": 0.0130339, // Execution time in seconds
        "mem_peak": 560848     // Peak memory usage in bytes
    },

    // Debug information set using Debugger::dump() function;
    // Only available in dev_mode
    "debug": [ 
        {
            "at": "File: codebase-root/api-funcs-base/@all.php; Line: 7",
            "value": "Hello from @all at the root of the API functions directory"
        }
    ]
}
<?php
Config::$dev_mode = false; // This will disable the debug and meta information in the response

Request:

https://api.sample.dev/ping?msg=ABCD

or

https://api.sample.dev/ping with POST data {"msg":"ABCD"}

or

https://api.sample.dev/ping/msg/ABCD

Response:

{
    "status": 0,
    "value": "Hello; Message received: ABCD"
}

Request:

https://api.sample.dev/example.adder?add[]=1&add[]=2&add[]=3

or

https://api.sample.dev/example.adder with POST data { "add" : [1,2,3] }

Response:

{
    "status": 0,
    "value": 6
}

Request:

https://api.sample.dev/does_not_exist

Response:

{
    "status": 1, // See src/StatusType.php for all possible status codes
    "status_desc": "Invalid_Request",
    "value": "API 'does_not_exist' not found"
}

Notes: