anshu-krishna / api-framework
API 框架
Requires
- php: >=8.1
- anshu-krishna/data-validator: ^2.8.1
- anshu-krishna/php-utilities: ^2.2
README
这是一个简单的API框架,可用于创建API。
安装
composer require anshu-krishna/api-framework
快速入门指南(使用提供的示例代码作为示例)
让我们创建一个简单的API服务器,并在过程中学习如何使用此框架。
查看示例目录以获取完整的示例服务器代码。
在此示例中,我们假设该服务器运行在https://api.sample.dev。
文件列表
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
列出文件的说明
-
codebase-root/
这是代码库的根目录。(选择任何目录)。 -
codebase-root/api-funcs-base/
这是存储所有API定义的目录。(目录名称不需要为api-funcs-base
。可以是任何名称)。-
@all.php
[可选] 此文件在执行此目录及其子目录中的所有API之前执行。 -
@index.php
[可选] 此文件定义了https://api.sample.dev上的API。 -
Ping.php
此文件定义了https://api.sample.dev/ping上的API。 -
Example/
此目录包含所有分组在'example' API下的API。-
@all.php
[可选] 此文件在执行此目录及其子目录中的所有API之前执行。 -
@index.php
[可选] 此文件定义了https://api.sample.dev/example上的API。 -
Adder.php
此文件定义了https://api.sample.dev/example.adder上的API。 -
CamelCase/
此目录包含所有分组在'example.camel_case' API下的API。-
@index.php
[可选] 此文件定义了https://api.sample.dev/example.camel_case上的API。 -
Hello.php
此文件定义了https://api.sample.dev/example.camel_case.hello上的API。
-
-
-
-
codebase-root/public/
这是服务器运行的公共目录。(即https://api.sample.dev必须指向此目录。)目录名称不需要为public
。可以是任何名称。-
.htaccess
此文件用于将URL重写为API框架。 -
index.php
此文件是服务器的入口点。
-
一些文件的示例代码(带说明/注释)
<?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']; });
查看示例目录以获取完整的示例服务器代码。
示例请求和响应
请求
https://api.sample.dev/ping
响应
{ "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
请求
https://api.sample.dev/ping?msg=ABCD
或
https://api.sample.dev/ping
与 POST 数据 {"msg":"ABCD"}
或
https://api.sample.dev/ping/msg/ABCD
响应
{ "status": 0, "value": "Hello; Message received: ABCD" }
请求
https://api.sample.dev/example.adder?add[]=1&add[]=2&add[]=3
或
https://api.sample.dev/example.adder
与 POST 数据 { "add" : [1,2,3] }
响应
{ "status": 0, "value": 6 }
请求
https://api.sample.dev/does_not_exist
响应
{ "status": 1, // See src/StatusType.php for all possible status codes "status_desc": "Invalid_Request", "value": "API 'does_not_exist' not found" }