art4/json-api-client
Composer 安装命令:
composer require art4/json-api-client
包简介
JSON API client
README 文档
README
JsonApiClient 👷♀️ is a PHP Library to validate and handle the response body from a JSON API Server.
Format: JSON API 1.0
🏁 Goals
- ✅ Be 100% JSON API 1.0 spec conform
- ⬜ Be open for new spec minor versions (see #90)
- ✅ Handle/validate a server response body
- ✅ Handle/validate a client request body
- ✅ Offer an easy way to retrieve the data
- ✅ Allow extendability and injection of classes/models
📦 Install
Via Composer
$ composer require art4/json-api-client
🏗️ Upgrade to v1
Version 1.0 is finally released. 🎉
After version 0.8.0 there where no breaking changes. Every change was backward compatible and every functionality that was removed in v1.0 only triggers a deprecation warning in v0.10.
To upgrade from v0.x to v1 just update to 0.10.2 and resolve all deprecation warnings.
Or in 3 simple steps:
- Update your composer.json to
"art4/json-api-client": "^0.10.2" - Make your code deprecation warnings free
- Upgrade your composer.json to
"art4/json-api-client": "^1.0"without breaking your app
(Compare the Symfony upgrade documentation)
🚀 Usage
See the quickstart guide or the documentation.
Using as parser
use Art4\JsonApiClient\Exception\InputException; use Art4\JsonApiClient\Exception\ValidationException; use Art4\JsonApiClient\Helper\Parser; // The Response body from a JSON API server $jsonapiString = '{"meta":{"info":"Testing the JsonApiClient library."}}'; try { // Use this if you have a response after calling a JSON API server $document = Parser::parseResponseString($jsonapiString); // Or use this if you have a request to your JSON API server $document = Parser::parseRequestString($jsonapiString); } catch (InputException $e) { // $jsonapiString is not valid JSON } catch (ValidationException $e) { // $jsonapiString is not valid JSON API }
Note: Using Art4\JsonApiClient\Helper\Parser is just a shortcut for directly using the Manager.
$document implements the Art4\JsonApiClient\Accessable interface to access the parsed data. It has the methods has($key), get($key) and getKeys().
// Note that has() and get() have support for dot-notated keys if ($document->has('meta.info')) { echo $document->get('meta.info'); // "Testing the JsonApiClient library." } // you can get all keys as an array var_dump($document->getKeys()); // array( // 0 => "meta" // )
Using as validator
JsonApiClient can be used as a validator for JSON API contents:
use Art4\JsonApiClient\Helper\Parser; $wrong_jsonapi = '{"data":{},"meta":{"info":"This is wrong JSON API. `data` has to be `null` or containing at least `type` and `id`."}}'; if ( Parser::isValidResponseString($wrong_jsonapi) ) { // or Parser::isValidRequestString($wrong_jsonapi) echo 'string is valid.'; } else { echo 'string is invalid json api!'; } // echoes 'string is invalid json api!'
Extend the client
Need more functionality? Want to directly inject your model? Easily extend JsonApiClient with the Factory.
🔊 Changelog
Please see CHANGELOG for more information what has changed recently.
🔧 Contributing
Please feel free to fork and sending Pull Requests. This project follows Semantic Versioning 2 and PER-CS2.0.
This projects comes with a docker-compose.yml where all tools for development are available.
Run docker compose build to build the image. Once you've build it, run docker compose up -d to start the container in the background.
Run docker compose exec -u 1000 php bash to use the bash inside the running container. There you can use all tools, e.g. composer with composer --version
Use exit to logout from the container and docker compose stop to stop the running container.
All following commands can be run inside the running docker container.
✅ Testing
Run PHPUnit for all tests:
$ composer run phpunit
Run PHPStan for static code analysis:
$ composer run phpstan
Let PHPUnit generate a HTLM code coverage report:
$ composer run coverage
You can find the code coverage report in .phpunit.cache/code-coverage/index.html.
✅ REUSE
The REUSE Helper tool makes licensing easy for humans and machines alike. It downloads the full license texts, adds copyright and license information to file headers, and contains a linter to identify problems.
Check all files for REUSE spec compliance:
composer run reuse-lint
Run this command to annotate PHP files in src and tests folders:
composer run reuse-annotate
❤️ Credits
📄 License
GPL3. Please see License File for more information.
art4/json-api-client 适用场景与选型建议
art4/json-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 805.27k 次下载、GitHub Stars 达 138, 最近一次更新时间为 2015 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「client」 「parser」 「validator」 「reader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 art4/json-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 art4/json-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 art4/json-api-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 805.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 139
- 点击次数: 21
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2015-08-11