https://github.com/nilportugues/php-api-problems
PSR7 Response implementation for the Problem Details for HTTP APIs
https://github.com/nilportugues/php-api-problems
api api-error api-problem api-problems error-handler error-handling error-messages errors json json-api microservice microservices php php7 psr-7 psr7-response xml
Last synced: 10 days ago
JSON representation
PSR7 Response implementation for the Problem Details for HTTP APIs
- Host: GitHub
- URL: https://github.com/nilportugues/php-api-problems
- Owner: nilportugues
- Created: 2016-03-08T23:20:23.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-02-26T14:36:05.000Z (about 4 years ago)
- Last Synced: 2025-04-15T02:19:04.671Z (about 1 month ago)
- Topics: api, api-error, api-problem, api-problems, error-handler, error-handling, error-messages, errors, json, json-api, microservice, microservices, php, php7, psr-7, psr7-response, xml
- Language: PHP
- Homepage: http://nilportugues.com
- Size: 31.3 KB
- Stars: 16
- Watchers: 1
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PSR7 HTTP APIs Problem Response
[](https://travis-ci.org/nilportugues/php-api-problems)
[](https://scrutinizer-ci.com/g/nilportugues/php-api-problems/?branch=master) [](https://insight.sensiolabs.com/projects/246f4e3d-4574-4dbb-82e5-00b0008bb11e)
[](https://packagist.org/packages/nilportugues/api-problems)
[](https://packagist.org/packages/nilportugues/api-problems)
[](https://packagist.org/packages/nilportugues/api-problems)
[](https://paypal.me/nilportugues)PSR7 Response implementation for the [Problem Details for HTTP APIs (RFC7807)](https://tools.ietf.org/html/rfc7807) specification.
## 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.
```php
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.**
```php
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.**
```php
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
In order to report more than problem, you must use the additional details parameter.
```php
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
**Headers**
```
HTTP/1.1 500 Bad Request
Content-Type: application/problem+json
```**Body**
```json
{
"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
**Headers**
```
HTTP/1.1 500 Bad Request
Content-Type: application/problem+xml
```**Body**
```xml
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.
Provided address is not a valid email.
```
---
## 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
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
* [Nil Portugués Calderó](http://nilportugues.com)
* [The Community Contributors](https://github.com/nilportugues/php-api-problems/graphs/contributors)## License
The code base is licensed under the [MIT license](LICENSE).