jpbassalot / jsonapi
一个简单的 JSON API,用于读取和分页目录中的 JSON 文件
v1.0.0
2023-04-03 17:17 UTC
Requires
- php: ^8.1
Requires (Dev)
- fakerphp/faker: ^1.21
- phpunit/phpunit: ^10.0
README
JSON 文件 API 是一个简单的只读 API,允许您获取和序列化存储在 JSON 文件中的数据。此 API 支持分页和根据特定属性搜索文件。
入门指南
要使用此 API,您需要安装 PHP >= 8.1。
使用方法
使用分页获取
示例: GET /path/to/index.php?page=1
查询参数
page(可选):要获取的页面号。如果未提供,默认为 1。
在 JSON 属性内搜索
要搜索特定属性,请使用带有搜索查询参数的 GET 请求。
示例: GET /path/to/index.php?search=data
查询参数
search(可选):用于过滤文件的搜索查询。
示例用法
require_once 'vendor/autoload.php'; use Jpbassalot\JsonApi; // A directory containing JSON files $dir = 'json'; $reports = new JsonApi($dir, 9); $all_files = $reports->getFiles(); $search = isset($_GET['search']) ? htmlspecialchars($_GET['search'], ENT_QUOTES, 'UTF-8') : ''; /** * JSON Example: * { * "title": "My Title", * "property1": "Property 1", * "property2": "Property 2", * "property3": "Property 3", * "nested": { * "property1": "Nested Property 1", * "property2": "Nested Property 2" * } */ if ($search) { $result = $reports->searchFilesData($search, ['title', 'property1', 'property2', 'property3', 'nested.property1', 'nested.property2'], $all_files); } else { $result = $reports->getFilesData($all_files); } echo $reports->sendJson($result, $all_files);