PHPackages                             crell/api-problem - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. crell/api-problem

ActiveLibrary[HTTP &amp; Networking](/categories/http)

crell/api-problem
=================

PHP wrapper for the api-problem IETF specification

3.8.0(3mo ago)2482.0M—7.3%2119MITPHPPHP ^8.3CI passing

Since Apr 15Pushed 3mo ago8 watchersCompare

[ Source](https://github.com/Crell/ApiProblem)[ Packagist](https://packagist.org/packages/crell/api-problem)[ Docs](https://github.com/Crell/ApiProblem)[ GitHub Sponsors](https://github.com/Crell)[ RSS](/packages/crell-api-problem/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (18)Used By (19)

ApiProblem
==========

[](#apiproblem)

[![Build Status](https://camo.githubusercontent.com/cedd886f49a3043a9b31e8005cc778e998b1ff174c0126dde562d413ba77ee7f/68747470733a2f2f7472617669732d63692e6f72672f4372656c6c2f41706950726f626c656d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Crell/ApiProblem)

This library provides a simple and straightforward implementation of the IETF Problem Details for HTTP APIs, [RFC 9457](https://tools.ietf.org/html/rfc9457).

[RFC 9457](https://tools.ietf.org/html/rfc9457) 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](https://tools.ietf.org/html/rfc9457) messages, in both JSON and XML variants.

Generating responses
--------------------

[](#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
-----------------

[](#sending-responses)

You're probably using [PSR-7](https://www.php-fig.org/psr/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](https://www.php-fig.org/psr/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
-------------------

[](#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
------------

[](#installation)

Install ApiProblem like any other Composer package:

```
composer require crell/api-problem

```

See the [Composer documentation](http://getcomposer.org/) for more details.

Security
--------

[](#security)

If you discover any security related issues, please use the [GitHub security reporting form](https://github.com/Crell/ApiProblem/security) rather than the issue queue.

Credits
-------

[](#credits)

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

License
-------

[](#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
------------

[](#contributing)

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

###  Health Score

69

—

FairBetter than 100% of packages

Maintenance80

Actively maintained with recent releases

Popularity58

Moderate usage in the ecosystem

Community35

Small or concentrated contributor base

Maturity87

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~292 days

Recently: every ~373 days

Total

17

Last Release

104d ago

Major Versions

1.7 → 2.02016-07-24

2.x-dev → 3.02018-11-24

PHP version history (6 changes)1.0-beta1PHP &gt;=5.3.3

2.0PHP ^5.5 || ^7.0

3.0PHP ^7.1

3.3PHP ^7.1 || ^8.0

3.7.0PHP ^7.4 || ^8.0

3.8.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/12e28c223b88445f07d697c8805bd856066c947f70b535f6a7e00d2cb311c3c2?d=identicon)[Crell](/maintainers/Crell)

---

Top Contributors

[![Crell](https://avatars.githubusercontent.com/u/254863?v=4)](https://github.com/Crell "Crell (137 commits)")[![nyamsprod](https://avatars.githubusercontent.com/u/51073?v=4)](https://github.com/nyamsprod "nyamsprod (9 commits)")[![raphaelstolt](https://avatars.githubusercontent.com/u/48225?v=4)](https://github.com/raphaelstolt "raphaelstolt (5 commits)")[![thomasvargiu](https://avatars.githubusercontent.com/u/732012?v=4)](https://github.com/thomasvargiu "thomasvargiu (4 commits)")[![sasezaki](https://avatars.githubusercontent.com/u/42755?v=4)](https://github.com/sasezaki "sasezaki (2 commits)")[![judgej](https://avatars.githubusercontent.com/u/395934?v=4)](https://github.com/judgej "judgej (2 commits)")[![simensen](https://avatars.githubusercontent.com/u/191200?v=4)](https://github.com/simensen "simensen (2 commits)")[![matason](https://avatars.githubusercontent.com/u/192593?v=4)](https://github.com/matason "matason (1 commits)")[![charjr](https://avatars.githubusercontent.com/u/102669158?v=4)](https://github.com/charjr "charjr (1 commits)")[![fbidu](https://avatars.githubusercontent.com/u/247856?v=4)](https://github.com/fbidu "fbidu (1 commits)")[![harikt](https://avatars.githubusercontent.com/u/120454?v=4)](https://github.com/harikt "harikt (1 commits)")[![klausi](https://avatars.githubusercontent.com/u/213229?v=4)](https://github.com/klausi "klausi (1 commits)")[![Apipa169](https://avatars.githubusercontent.com/u/44235469?v=4)](https://github.com/Apipa169 "Apipa169 (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

apiproblemjsonphpphp-libraryrfc-7807xmlhttpjsonrestxmlapi-problem

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/crell-api-problem/health.svg)

```
[![Health](https://phpackages.com/badges/crell-api-problem/health.svg)](https://phpackages.com/packages/crell-api-problem)
```

###  Alternatives

[jacwright/restserver

php rest server for very light-weight REST APIs

51070.0k3](/packages/jacwright-restserver)[vinelab/http

An http library developed for the laravel framework. aliases itself as HttpClient

59300.2k11](/packages/vinelab-http)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69114.3k](/packages/serpapi-google-search-results-php)[hgg/pardot

Pardot API library for building custom CRM connectors

2399.6k](/packages/hgg-pardot)[jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

2425.9k1](/packages/jsor-hal-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
