PHP wrapper for the api-problem IETF specification

Fund package maintenance!

Installs: 1 617 344

Dependents: 23

Suggesters: 1

Security: 0

Stars: 239

Watchers: 10

Forks: 21

Open Issues: 1

3.6.1 2022-01-04 15:47 UTC

This package is auto-updated.

Last update: 2024-06-10 20:22:14 UTC


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.", "");
// Defined properties in the API have their own setter methods.
  ->setDetail("Your current balance is 30, but that costs 50.")
// But you can also support any arbitrary extended properties!
$problem['balance'] = 30;
$problem['accounts'] = [

$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!)


Install ApiProblem like any other Composer package:

composer require crell/api-problem

See the Composer documentation for more details.


If you discover any security related issues, please use the GitHub security reporting form rather than the issue queue.


  • [Larry Garfield][link-author]
  • [All Contributors][link-contributors]


This library is released under the MIT license. In short, "leave the copyright statement intact, otherwise have fun." See LICENSE for more information.


Pull requests accepted! The goal is complete conformance with the IETF spec.