PHPackages                             nilportugues/api-problems - 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. [API Development](/categories/api)
4. /
5. nilportugues/api-problems

ActiveLibrary[API Development](/categories/api)

nilportugues/api-problems
=========================

PSR7 Response implementation for the Problem Details for HTTP APIs

1.2.3(9y ago)1749.6k4[1 PRs](https://github.com/nilportugues/php-api-problems/pulls)2MITPHP

Since Mar 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/nilportugues/php-api-problems)[ Packagist](https://packagist.org/packages/nilportugues/api-problems)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-api-problems/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (5)Versions (7)Used By (2)

PSR7 HTTP APIs Problem Response
===============================

[](#psr7-http-apis-problem-response)

[![Build Status](https://camo.githubusercontent.com/632ce5bd7f43de32b1de85fe4637913dbc2e2ba7a2b002a5bac87bb8ab86aadc/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d6170692d70726f626c656d732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/nilportugues/php-api-problems)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/bcaa70eb8e96940f2b2e9c7599253196ecc86e09d6d403c17d3e715d2e3174d9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f7068702d6170692d70726f626c656d732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/php-api-problems/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/9c6c2e8c5b837303ada0b44899394e7d1b5f022f1adae1a331ca8e417edbea01/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32343666346533642d343537342d346462622d383265352d3030623030303862623131652f6d696e692e706e673f)](https://insight.sensiolabs.com/projects/246f4e3d-4574-4dbb-82e5-00b0008bb11e)[![Latest Stable Version](https://camo.githubusercontent.com/036665c025728fdb1f61dd8c59d16f80634ae7b8db108208efb58972c68b07a1/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d70726f626c656d732f762f737461626c65)](https://packagist.org/packages/nilportugues/api-problems)[![Total Downloads](https://camo.githubusercontent.com/0ee58fd13d3e23a419d141dde0c782ffc2bdf255540f86cb6c1e5a6bfb2fd85c/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d70726f626c656d732f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/api-problems)[![License](https://camo.githubusercontent.com/61d76509a847a790b11aa8fce78281fd9f3fc9da335cd88ca1030ddb36070431/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d70726f626c656d732f6c6963656e7365)](https://packagist.org/packages/nilportugues/api-problems)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

PSR7 Response implementation for the [Problem Details for HTTP APIs (RFC7807)](https://tools.ietf.org/html/rfc7807) specification.

Usage
-----

[](#usage)

To report a single error, all you need to do is pass in the mandatory parameters and you'll be fine.

**Straightforward usage (recommended)**

This is probably the fastest way and it's really convenient as it hides the presenter and creating the instances from you.

```
use NilPortugues\Api\Problem\ApiProblemResponse;

$additionalDetails = []; //you may pass additional details too.

/**@var $response is a PSR7 response */
$response = ApiProblemResponse::json(404,'User with id 5 not found.', 'Not Found', 'user.not_found', $additionalDetails);
$response = ApiProblemResponse::xml(404,'User with id 5 not found.', 'Not Found', 'user.not_found', $additionalDetails);

$response = ApiProblemResponse::fromExceptionToJson($exception);
$response = ApiProblemResponse::fromExceptionToXml($exception);
```

**Using the constructor and handling the response yourself.**

```
use NilPortugues\Api\Problem\ApiProblem;
use NilPortugues\Api\Problem\ApiProblemResponse;
use NilPortugues\Api\Problem\Presenter\JsonPresenter;

$apiProblem = new ApiProblem(
    404,
    'User with id 5 not found.',
    'Not Found',
    'user.not_found'
);

$presenter = new JsonPresenter($apiProblem); //or XmlPresenter
return new ApiProblemResponse($presenter);
```

**Using an Exception and handling the response yourself.**

```
use NilPortugues\Api\Problem\ApiProblem;
use NilPortugues\Api\Problem\ApiProblemResponse;
use NilPortugues\Api\Problem\Presenter\JsonPresenter;

try {
    //...your code throwing an exception
    throw new \Exception('User with id 5 not found.', 404);

} catch(\Exception $exception) {

    $problem = ApiProblem::fromException($exception);
    $presenter = new JsonPresenter($apiProblem); //or XmlPresenter
    return new ApiProblemResponse($presenter);
}
```

Multiple Problems, one object
-----------------------------

[](#multiple-problems-one-object)

In order to report more than problem, you must use the additional details parameter.

```
use NilPortugues\Api\Problem\ApiProblem;
use NilPortugues\Api\Problem\ApiProblemResponse;
use NilPortugues\Api\Problem\Presenter\JsonPresenter;

try {
    // some code of yours throws an exception... for instance:
    throw new \Exception('User data is not valid.', 500);

} catch(\Exception $exception) {

    $additionalDetails = [
        'errors' => [
            ['name' => 'username', 'error' => 'Username must be at least 5 characters long.'],
            ['name' => 'email', 'error' => 'Provided address is not a valid email.'],
        ],
    ]

    $apiProblem = ApiProblem::fromException(
        $exception,
        'Input values do not match the requirements',
        'user.invalid_data',
        $additionalDetails;
    );

    $presenter = new JsonPresenter($apiProblem); //or XmlPresenter

    return new ApiProblemResponse($presenter);
}
```

#### JSON Output

[](#json-output)

**Headers**

```
HTTP/1.1 500 Bad Request
Content-Type: application/problem+json

```

**Body**

```
{
    "title": "Input values do not match the requirements",
    "status": 500,
    "detail": "User data is not valid.",
    "type": "user.invalid_data",
    "errors": [
        {
            "name": "username",
            "error": "Username must be at least 5 characters long."
        },
        {
            "name": "email",
            "error": "Provided address is not a valid email."
        }
    ]
}
```

#### XML Output

[](#xml-output)

**Headers**

```
HTTP/1.1 500 Bad Request
Content-Type: application/problem+xml

```

**Body**

```

  Input values do not match the requirements
  500
  User data is not valid.
  user.invalid_data

      username
      Username must be at least 5 characters long.

      email
      Provided address is not a valid email.

```

---

Contribute
----------

[](#contribute)

Contributions to the package are always welcome!

- Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-api-problems/issues/new).
- You can grab the source code at the package's [Git repository](https://github.com/nilportugues/php-api-problems).

Support
-------

[](#support)

Get in touch with me using one of the following means:

- Emailing me at
- Opening an [Issue](https://github.com/nilportugues/php-api-problems/issues/new)

Authors
-------

[](#authors)

- [Nil Portugués Calderó](http://nilportugues.com)
- [The Community Contributors](https://github.com/nilportugues/php-api-problems/graphs/contributors)

License
-------

[](#license)

The code base is licensed under the [MIT license](LICENSE).

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~27 days

Total

6

Last Release

3632d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/beba5d66ff48128da92c91850e69b9294768718bcdc12e61cc9ee8453d0d6b69?d=identicon)[nilportugues](/maintainers/nilportugues)

---

Top Contributors

[![nilportugues](https://avatars.githubusercontent.com/u/550948?v=4)](https://github.com/nilportugues "nilportugues (23 commits)")

---

Tags

apiapi-errorapi-problemapi-problemserror-handlererror-handlingerror-messageserrorsjsonjson-apimicroservicemicroservicesphpphp7psr-7psr7-responsexmlresponsejsonapixmlpsr7transformer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nilportugues-api-problems/health.svg)

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

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69735.1M159](/packages/algolia-algoliasearch-client-php)[theodo-group/llphant

LLPhant is a library to help you build Generative AI applications.

1.7k409.0k6](/packages/theodo-group-llphant)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)[nilportugues/laravel5-json-api

Laravel 5 JSON API Transformer Package

31232.5k1](/packages/nilportugues-laravel5-json-api)[nilportugues/jsonapi-bundle

Symfony 2 &amp; 3 JSON API Transformer Package

11446.7k](/packages/nilportugues-jsonapi-bundle)

PHPackages © 2026

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