anshu-krishna/api-framework

API 框架

11.5.2 2024-09-26 03:34 UTC

This package is auto-updated.

Last update: 2024-09-26 03:35:44 UTC


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。可以是任何名称)。

  • 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"
}

注意