https://github.com/zircote/apiproblem
A PHP Exception to provide the functionality of the `application/api-problem` RFC proposal. This class creates simple JSON [RFC4627] and XML [W3C.REC-xml-20081126] document formats to suit the purpose described in [Problem Details](http://tools.ietf.org/html/draft-nottingham-http-problem).
https://github.com/zircote/apiproblem
Last synced: about 1 year ago
JSON representation
A PHP Exception to provide the functionality of the `application/api-problem` RFC proposal. This class creates simple JSON [RFC4627] and XML [W3C.REC-xml-20081126] document formats to suit the purpose described in [Problem Details](http://tools.ietf.org/html/draft-nottingham-http-problem).
- Host: GitHub
- URL: https://github.com/zircote/apiproblem
- Owner: zircote
- License: apache-2.0
- Created: 2013-06-19T17:42:23.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-12-16T13:57:23.000Z (over 12 years ago)
- Last Synced: 2025-03-23T19:45:14.764Z (over 1 year ago)
- Language: PHP
- Size: 130 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE-2.0
Awesome Lists containing this project
README
# ApiProblem
- [](http://travis-ci.org/zircote/ApiProblem) `master`
`ApiProblem` is an attempt to provide the functionality and problem reporting as defined in
[Problem Details for HTTP APIs](http://tools.ietf.org/html/draft-nottingham-http-problem). The goal being a simple
Exception wrapper for PHP that can send the desired response in `JSON` and `XML`.
## Use
#### The `sendHttpResponse` method
The `sendHTTPResponse` has two parameter, both optional `$format` and `$terminate`.
- `format`:
- `ApiProblem::FORMAT_XML`: `application/api-problem+json`
- `ApiProblem::FORMAT_JSON`: `application/api-problem+xml`
- `terminate`: `bool`
- `true` (default) headers are sent and execution is terminated.
- `false` the body payload is returned
#### JSON Example
```php
sendHTTPResponse(ApiProblem::FORMAT_JSON);
```
#### Result
```http
HTTP/1.0 400 Bad Request
Access-Control-Allow-Origin: *
Content-Type: application/api-problem+json
Link: ; rel="http://api-problem.domain.com/some-url.html" title="Bad Request"
{
"problemType": "http://api-problem.domain.com/some-url.html",
"title": "Bad Request",
"httpStatus": 400,
"detail": "some detail",
"problemInstance": "http://domain.com/this-request"
}
```
#### XML Example
```php
sendHTTPResponse(ApiProblem::FORMAT_XML);
}
```
#### Result
```http
HTTP/1.0 400 Bad Request
Access-Control-Allow-Origin: *
Content-Type: application/api-problem+xml
Link: ; rel="http://api-problem.domain.com/some-url.html"; title="Bad Request"
http://api-problem.domain.com/some-url.html
Bad Request
400
some detail
http://domain.com/this-request
```
#### Adding additional headers, i.e. CORS, Auth, etc
```php
setHeader('Access-Control-Allow-Origin', '*');
$apiProblem->setHeader('WWW-Authenticate', 'bearer realm="my_realm"');
$apiProblem->sendHTTPResponse(ApiProblem::FORMAT_JSON);
```
#### Result
```http
HTTP/1.0 401 Unauthorized
Access-Control-Allow-Origin: *
WWW-Authenticate: bearer realm="my_realm"
Content-Type: application/api-problem+json
Link: ; rel="http://api-problem.domain.com/some-url.html" title="Bad Request"
{
"problemType": "http://api-problem.domain.com/some-url.html",
"title": "Unauthorized",
"httpStatus": 401,
"detail": "some detail",
"problemInstance": "http://domain.com/this-request"
}
```
#### Adding extended data to the problem instance:
```php
setExtensionData('ext_test', 'etx_test_value');
$apiProblem->setExtensionData('ext_test_array_i', array('a' => 'a_d', 'b' => 'b_d', 'c' => 'c_d'));
$apiProblem->setExtensionData('ext_test_array_ni', array('a', 'b', 'c'));
$apiProblem->sendHTTPResponse(ApiProblem::FORMAT_JSON);
```
#### Result
```http
HTTP/1.0 401 Unauthorized
Content-Type: application/api-problem+json
Link: ; rel="http://api-problem.domain.com/some-url.html" title="Bad Request"
{
"problemType": "http://api-problem.domain.com/some-url.html",
"title": "Unauthorized",
"httpStatus": 401,
"detail": "some detail",
"problemInstance": "http://domain.com/this-request",
"ext_test":"etx_test_value",
"ext_test_array_i":{
"a":"a_d",
"b":"b_d",
"c":"c_d"
}
"ext_test_array_ni":[
"a",
"b",
"c"
]
}
```
[](https://bitdeli.com/free "Bitdeli Badge")