Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple implementation of the api-problem specification
branch: master

Merge pull request #10 from mdwheele/patch-1

Makes codeclimate/php-test-reporter a dev dependency.
latest commit fe2daa96f9
@Crell authored


Build Status

This library provides a simple and straightforward implementation of the IETF API-Problem specification, currently in draft 7.

API-Problem 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 API-Problem 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!

$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'] = array(

$json_string = $problem->asJson();

// Now send that JSON string as a response along with the appropriate HTTP error
// code.  Also check out asXml() for the angle-bracket fans in the room.

Or, even better, you can subclass ApiProblem for a specific problemType (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!

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;
use Crell\ApiProblem\RequiredPropertyNotFoundException;

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


The preferred method of installation is via Composer with the following command:

composer require crell/api-problem

See the Composer documentation for more details.

Alternatively, clone the project and install into your project manually.


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. This library will be updated as needed for future drafts.

Something went wrong with that request. Please try again.