|
| 1 | +# API Framework |
| 2 | + |
| 3 | + |
| 4 | +This is a simple API framework that can be used to create APIs. |
| 5 | + |
| 6 | + |
| 7 | +## Installation: |
| 8 | +```bash |
| 9 | +composer require anshu-krishna/api-framework |
| 10 | +``` |
| 11 | + |
| 12 | +## Quick Start (using the provides sample code as example): |
| 13 | + |
| 14 | +Let's create a simple API server and on the way learn how to use this framework. |
| 15 | + |
| 16 | +See the [sample](./sample/) directory for the full sample server code. |
| 17 | + |
| 18 | +In this example we will assume that this server is running at https://api.sample.dev . |
| 19 | + |
| 20 | +### Files List: |
| 21 | +``` |
| 22 | +codebase-root/ |
| 23 | +├─ api-funcs-base/ |
| 24 | +│ ├─ @all.php |
| 25 | +│ ├─ @index.php |
| 26 | +│ ├─ Ping.php |
| 27 | +│ ├─ Example/ |
| 28 | +│ │ ├─ @all.php |
| 29 | +│ │ ├─ @index.php |
| 30 | +│ │ ├─ Adder.php |
| 31 | +│ │ ├─ CamelCase/ |
| 32 | +│ │ │ ├─ @index.php |
| 33 | +│ │ │ ├─ Hello.php |
| 34 | +├─ public/ |
| 35 | +│ ├─ .htaccess |
| 36 | +│ ├─ index.php |
| 37 | +``` |
| 38 | + |
| 39 | +#### Explanation of the listed files: |
| 40 | + |
| 41 | +- `codebase-root/` is the root directory of the codebase. Choose any directory. |
| 42 | + |
| 43 | +- `codebase-root/api-funcs-base/` is the directory where all the API functions are stored. *This directory can be named anything.* |
| 44 | + |
| 45 | + - `@all.php` [Optional] is the file that is called for all the functions in this directory ***and its subdirectories***. |
| 46 | + |
| 47 | + - `@index.php` [Optional] File that defines the api available at https://api.sample.dev |
| 48 | + |
| 49 | + - `Ping.php` is the file that defines the api available at https://api.sample.dev/ping |
| 50 | + |
| 51 | + - `Example/` is a directory that contains more API functions. |
| 52 | + |
| 53 | + - `@all.php` [Optional] is the file that is called for all the functions in this directory ***and its subdirectories***. |
| 54 | + |
| 55 | + - `@index.php` [Optional] File that defines the api available at https://api.sample.dev/example |
| 56 | + |
| 57 | + - `Adder.php` is the file that defines the api available at https://api.sample.dev/example.adder |
| 58 | + |
| 59 | + - `CamelCase/` is a directory that contains more API functions. |
| 60 | + |
| 61 | + - `@index.php` [Optional] File that defines the api available at https://api.sample.dev/example.camel_case |
| 62 | + |
| 63 | + - `Hello.php` is the file that defines the api available at https://api.sample.dev/example.camel_case.hello |
| 64 | + |
| 65 | + |
| 66 | +- `codebase-root/public/` is the public directory where the server is running. *This directory can be named anything.* |
| 67 | + |
| 68 | + - `.htaccess` is the file that redirects all requests to `index.php`. |
| 69 | + |
| 70 | + - `index.php` is the file that initializes the API framework. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +#### Sample code for some of the files (with explanations/comments): |
| 75 | + |
| 76 | +```php |
| 77 | +<?php // file: codebase-root/public/index.php |
| 78 | + |
| 79 | +require_once '../vendor/autoload.php'; |
| 80 | + |
| 81 | +use Krishna\API\Config; |
| 82 | +use Krishna\API\Server; |
| 83 | + |
| 84 | +Config::$dev_mode = true; // Set to false in production |
| 85 | +Config::$zlib = false; // Set to true if you want to compress the output |
| 86 | + |
| 87 | +// See Krishna\API\Config for more options |
| 88 | + |
| 89 | + |
| 90 | +// Initialize the server |
| 91 | +Server::init( |
| 92 | + func_base_path: __DIR__ . '/../api-funcs-base', |
| 93 | +); |
| 94 | + |
| 95 | +// Start executing api request |
| 96 | +Server::execute(); |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +```php |
| 101 | +<?php // file: codebase-root/api-funcs-base/Ping.php |
| 102 | + |
| 103 | +// This API expects either no parameters or a single parameter 'msg' of type string; |
| 104 | +// See DataValidator in the notes below to see more examples of possible signatures |
| 105 | + |
| 106 | +use Krishna\API\Func; |
| 107 | + |
| 108 | +// Set the signature of the function |
| 109 | +Func::set_signature([ |
| 110 | + '?msg' => 'string', |
| 111 | +]); |
| 112 | + |
| 113 | + |
| 114 | +// Set the definition of the function |
| 115 | +Func::set_definition(function(array $data, string $funcName) { |
| 116 | + if(!array_key_exists('msg', $data)) { |
| 117 | + return 'Hello; No message received'; |
| 118 | + } |
| 119 | + return 'Hello; Message received: ' . $data['msg']; |
| 120 | +}); |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +See the [sample](./sample/) directory for the full sample server code. |
| 125 | + |
| 126 | +___ |
| 127 | + |
| 128 | +### Sample Requests and Responses: |
| 129 | + |
| 130 | +#### Request: |
| 131 | +`https://api.sample.dev/ping` |
| 132 | + |
| 133 | +#### Response: |
| 134 | +```json |
| 135 | +{ |
| 136 | + "status": 0, // 0 = Success |
| 137 | + "value": "Hello; No message received", |
| 138 | + |
| 139 | + // Meta information; Only available in dev_mode |
| 140 | + "meta": { |
| 141 | + "exe_time": 0.0130339, // Execution time in seconds |
| 142 | + "mem_peak": 560848 // Peak memory usage in bytes |
| 143 | + }, |
| 144 | + |
| 145 | + // Debug information set using Debugger::dump() function; |
| 146 | + // Only available in dev_mode |
| 147 | + "debug": [ |
| 148 | + { |
| 149 | + "at": "File: codebase-root/api-funcs-base/@all.php; Line: 7", |
| 150 | + "value": "Hello from @all at the root of the API functions directory" |
| 151 | + } |
| 152 | + ] |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +```php |
| 157 | +<?php |
| 158 | +Config::$dev_mode = false; // This will disable the debug and meta information in the response |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +#### Request: |
| 164 | + |
| 165 | +`https://api.sample.dev/ping?msg=ABCD` |
| 166 | + |
| 167 | +or |
| 168 | + |
| 169 | +`https://api.sample.dev/ping` with POST data `{"msg":"ABCD"}` |
| 170 | + |
| 171 | +or |
| 172 | + |
| 173 | +`https://api.sample.dev/ping/msg/ABCD` |
| 174 | + |
| 175 | +#### Response: |
| 176 | +```json |
| 177 | +{ |
| 178 | + "status": 0, |
| 179 | + "value": "Hello; Message received: ABCD" |
| 180 | +} |
| 181 | +``` |
| 182 | +--- |
| 183 | + |
| 184 | +#### Request: |
| 185 | + |
| 186 | +`https://api.sample.dev/example.adder?add[]=1&add[]=2&add[]=3` |
| 187 | + |
| 188 | +or |
| 189 | + |
| 190 | +`https://api.sample.dev/example.adder` with POST data `{ "add" : [1,2,3] }` |
| 191 | + |
| 192 | +#### Response: |
| 193 | +```json |
| 194 | +{ |
| 195 | + "status": 0, |
| 196 | + "value": 6 |
| 197 | +} |
| 198 | +``` |
| 199 | +--- |
| 200 | + |
| 201 | +#### Request: |
| 202 | + |
| 203 | +`https://api.sample.dev/does_not_exist` |
| 204 | + |
| 205 | +#### Response: |
| 206 | +```json |
| 207 | +{ |
| 208 | + "status": 1, // See src/StatusType.php for all possible status codes |
| 209 | + "status_desc": "Invalid_Request", |
| 210 | + "value": "API 'does_not_exist' not found" |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +#### Notes: |
| 217 | + |
| 218 | +- [GitHub DataValidator](https://github.com/anshu-krishna/PHP-Data-Validator) |
0 commit comments