• Stars
    star
    239
  • Rank 168,763 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple implementation of the api-problem specification. Includes PSR-15 support.

ApiProblem

Build Status

This library provides a simple and straightforward implementation of the IETF Problem Details for HTTP APIs, RFC 7807.

RFC 7807 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 7807 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.

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.

More Repositories

1

Serde

Robust Serde (serialization/deserialization) library for PHP 8.
PHP
268
star
2

Tukio

A complete and robust implementation of the PSR-14 EventDispatcher specification.
PHP
99
star
3

AttributeUtils

Utilities to help ease parsing and manging of attributes
PHP
90
star
4

EnvMapper

Easily map environment variables into defined classed objects, ready for Dependency Injection.
PHP
83
star
5

enum-comparison

A comparison of enumerations and similar features in different languages
Rust
79
star
6

fp

Utilities for functional tasks in PHP, especially pipelining
PHP
59
star
7

Transformer

PHP
31
star
8

mastobot

Simple personal scheduling bot for Mastodon accounts
PHP
22
star
9

HtmlModel

Domain value objects for modeling HTML pages
PHP
21
star
10

butler

Skunkworks for the Butler initiative for Drupal 8
PHP
15
star
11

Cache-old

Temporary home for the PSR\Cache libraries
PHP
14
star
12

stacker

PHP
12
star
13

DBTNG

The DBTNG database abstraction library for PHP
10
star
14

plugin

Development for the new Drupal plugins system
PHP
9
star
15

Eliza

The Eliza chatbot therapist.
PHP
7
star
16

php-project-template

My personal template for new PHP projects.
Makefile
7
star
17

EnumTools

A collection of useful PHP enumerations for PHP 8.1 and later.
PHP
7
star
18

Config

A simple but powerful configuration loader, based on classed objects.
PHP
6
star
19

aoc2021

Entries for Advent of Code 2021
PHP
5
star
20

slides-cloud-friendly

JavaScript
3
star
21

drupal-oo-hooks

PHP
3
star
22

joindinaudit

PHP
3
star
23

slides-management-60s

Slides for "Project management lessons from the 1960s"
JavaScript
3
star
24

webbspace7

Drupal 7 version of WeBBspace
PHP
3
star
25

OrderedCollection

A fast and robust library for priority and topological sorting.
PHP
3
star
26

Document

This will get renamed eventually, once I pick a name.
PHP
3
star
27

TransformerBundle

PHP
2
star
28

slides-never-use-arrays

Never use arrays presentation.
JavaScript
2
star
29

slides-wscci

The ever-evolving slide deck of WSCCI
JavaScript
2
star
30

zero-to-silex

PHP
2
star
31

ArgParser

Modern CLI argument parsing
PHP
2
star
32

slides-php7

CSS
1
star
33

sorting-benchmarks

Topological sorting for PHP.
PHP
1
star
34

Xavier

Next Generation XML handling for PHP.
PHP
1
star
35

php-rfcs

Random pages associated with ongoing RFC work.
PHP
1
star
36

PGTools

Messing around with Postgresql
PHP
1
star
37

Denver-Architecture

CSS
1
star
38

webbspace5

WeBBspace for Drupal 5
PHP
1
star
39

sfl2012-drupal8

Symfony2 meets Drupal 8
JavaScript
1
star
40

psr-hug

PHP
1
star
41

serializer-test

Just fiddling around with the Symfony Serializer. Ignore this.
PHP
1
star
42

webbspace6

WeBBspace for Drupal 6
PHP
1
star
43

PlanedoBundle

Feed aggregator bundle for Symfony 5.4 and later.
PHP
1
star
44

slides-d8-for-devs

CSS
1
star
45

Cache-Util

Useful utility classes and traits for implementing the PSR cache standard
PHP
1
star
46

slides-php80

Slides for Exploring PHP 8.0 presentation
JavaScript
1
star