crell/api-problem
Composer 安装命令:
composer require crell/api-problem
包简介
PHP wrapper for the api-problem IETF specification
关键字:
README 文档
README
This library provides a simple and straightforward implementation of the IETF Problem Details for HTTP APIs, RFC 9457.
RFC 9457 is a simple specification for formatting error responses from RESTful APIs on the web. This library provides a simple and convenient way to interact with that specification. It supports generating and parsing RFC 9457 messages, in both JSON and XML variants.
Generating responses
What's that you say? Someone sent your API a bad request? Tell them it's a problem!
use Crell\ApiProblem\ApiProblem; $problem = new ApiProblem("You do not have enough credit.", "http://example.com/probs/out-of-credit"); // Defined properties in the API have their own setter methods. $problem ->setDetail("Your current balance is 30, but that costs 50.") ->setInstance("http://example.net/account/12345/msgs/abc"); // But you can also support any arbitrary extended properties! $problem['balance'] = 30; $problem['accounts'] = [ "http://example.net/account/12345", "http://example.net/account/67890" ]; $json_string = $problem->asJson(); // Now send that JSON string as a response along with the appropriate HTTP error // code and content type which is available via ApiProblem::CONTENT_TYPE_JSON. // Also check out asXml() and ApiProblem::CONTENT_TYPE_XML for the angle-bracket fans in the room.
Or, even better, you can subclass ApiProblem for a specific problem type (since the type and title are supposed to go together and be relatively fixed), then just populate your own error-specific data. Just like extending an exception!
If you're using a library or framework that wants to do its own JSON serialization, that's also fully supported. ApiProblem implements\JsonSerializable, so you can pass it directly to json_encode() as if it were a naked array.
$response = new MyFrameworksJsonResponse($problem); // Or do it yourself $body = json_encode($problem);
Sending Responses
You're probably using PSR-7 for your responses. That's why this library includes a utility to convert your ApiProblem object to a PSR-7 ResponseInterface object, using a PSR-17 factory of your choice. Like so:
use Crell\ApiProblem\HttpConverter; $factory = getResponseFactoryFromSomewhere(); // The second parameter says whether to pretty-print the output. $converter = new HttpConverter($factory, true); $response = $converter->toJsonResponse($problem); // or $response = $converter->toXmlResponse($problem);
That gives back a fully-functional and marked Response object, ready to send back to the client.
Receiving responses
Are you sending messages to an API that is responding with API-Problem errors? No problem! You can easily handle that response like so:
use Crell\ApiProblem\ApiProblem; $problem = ApiProblem::fromJson($some_json_string); $title = $problem->getTitle(); $type = $problem->getType(); // Great, now we know what went wrong, so we can figure out what to do about it.
(It works for fromXml(), too!)
Installation
Install ApiProblem like any other Composer package:
composer require crell/api-problem
See the Composer documentation for more details.
Security
If you discover any security related issues, please use the GitHub security reporting form rather than the issue queue.
Credits
- [Larry Garfield][link-author]
- [All Contributors][link-contributors]
License
This library is released under the MIT license. In short, "leave the copyright statement intact, otherwise have fun." See LICENSE for more information.
Contributing
Pull requests accepted! The goal is complete conformance with the IETF spec.
crell/api-problem 适用场景与选型建议
crell/api-problem 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.07M 次下载、GitHub Stars 达 248, 最近一次更新时间为 2013 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「xml」 「http」 「rest」 「api-problem」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 crell/api-problem 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crell/api-problem 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 crell/api-problem 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Load DOM document safety
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 2.07M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 251
- 点击次数: 24
- 依赖项目数: 26
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2013-04-15