{"id":15664159,"url":"https://github.com/nilportugues/php-api-problems","last_synced_at":"2025-10-08T20:50:39.735Z","repository":{"id":54285360,"uuid":"53452338","full_name":"nilportugues/php-api-problems","owner":"nilportugues","description":"PSR7 Response implementation for the Problem Details for HTTP APIs","archived":false,"fork":false,"pushed_at":"2021-02-26T14:36:05.000Z","size":32,"stargazers_count":17,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-20T01:11:51.696Z","etag":null,"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"],"latest_commit_sha":null,"homepage":"http://nilportugues.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilportugues.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-08T23:20:23.000Z","updated_at":"2025-04-16T03:48:50.000Z","dependencies_parsed_at":"2022-08-13T11:00:41.586Z","dependency_job_id":null,"html_url":"https://github.com/nilportugues/php-api-problems","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/nilportugues/php-api-problems","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-problems","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-problems/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-problems/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-problems/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilportugues","download_url":"https://codeload.github.com/nilportugues/php-api-problems/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-problems/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000638,"owners_count":26082819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["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"],"created_at":"2024-10-03T13:41:28.308Z","updated_at":"2025-10-08T20:50:39.697Z","avatar_url":"https://github.com/nilportugues.png","language":"PHP","readme":"# PSR7 HTTP APIs Problem Response \n\n[![Build Status](https://travis-ci.org/nilportugues/php-api-problems.svg?branch=master)](https://travis-ci.org/nilportugues/php-api-problems)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/php-api-problems/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/php-api-problems/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/246f4e3d-4574-4dbb-82e5-00b0008bb11e/mini.png?)](https://insight.sensiolabs.com/projects/246f4e3d-4574-4dbb-82e5-00b0008bb11e) \n[![Latest Stable Version](https://poser.pugx.org/nilportugues/api-problems/v/stable)](https://packagist.org/packages/nilportugues/api-problems) \n[![Total Downloads](https://poser.pugx.org/nilportugues/api-problems/downloads)](https://packagist.org/packages/nilportugues/api-problems) \n[![License](https://poser.pugx.org/nilportugues/api-problems/license)](https://packagist.org/packages/nilportugues/api-problems) \n[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)\n\nPSR7 Response implementation for the [Problem Details for HTTP APIs (RFC7807)](https://tools.ietf.org/html/rfc7807)  specification.  \n\n## Usage\n \nTo report a single error, all you need to do is pass in the mandatory parameters and you'll be fine.\n\n**Straightforward usage (recommended)**\n\nThis is probably the fastest way and it's really convenient as it hides the presenter and creating the instances from you.\n\n```php\nuse NilPortugues\\Api\\Problem\\ApiProblemResponse;\n\n$additionalDetails = []; //you may pass additional details too.\n\n/**@var $response is a PSR7 response */\n$response = ApiProblemResponse::json(404,'User with id 5 not found.', 'Not Found', 'user.not_found', $additionalDetails);\n$response = ApiProblemResponse::xml(404,'User with id 5 not found.', 'Not Found', 'user.not_found', $additionalDetails);\n\n$response = ApiProblemResponse::fromExceptionToJson($exception);\n$response = ApiProblemResponse::fromExceptionToXml($exception);\n```\n\n**Using the constructor and handling the response yourself.**\n\n```php\nuse NilPortugues\\Api\\Problem\\ApiProblem;\nuse NilPortugues\\Api\\Problem\\ApiProblemResponse;\nuse NilPortugues\\Api\\Problem\\Presenter\\JsonPresenter;\n\n$apiProblem = new ApiProblem(\n    404,\n    'User with id 5 not found.',\n    'Not Found', \n    'user.not_found'\n); \n\n$presenter = new JsonPresenter($apiProblem); //or XmlPresenter\nreturn new ApiProblemResponse($presenter);  \n```\n\n**Using an Exception and handling the response yourself.**\n\n```php\nuse NilPortugues\\Api\\Problem\\ApiProblem;\nuse NilPortugues\\Api\\Problem\\ApiProblemResponse;\nuse NilPortugues\\Api\\Problem\\Presenter\\JsonPresenter;\n\ntry {\n    //...your code throwing an exception\n    throw new \\Exception('User with id 5 not found.', 404);   \n     \n} catch(\\Exception $exception) {\n\n    $problem = ApiProblem::fromException($exception);\n    $presenter = new JsonPresenter($apiProblem); //or XmlPresenter\n    return new ApiProblemResponse($presenter);        \n}\n```\n\n## Multiple Problems, one object\n\nIn order to report more than problem, you must use the additional details parameter.\n \n```php\nuse NilPortugues\\Api\\Problem\\ApiProblem;\nuse NilPortugues\\Api\\Problem\\ApiProblemResponse;\nuse NilPortugues\\Api\\Problem\\Presenter\\JsonPresenter;\n\ntry {\n    // some code of yours throws an exception... for instance:\n    throw new \\Exception('User data is not valid.', 500);\n           \n} catch(\\Exception $exception) {\n\n    $additionalDetails = [\n        'errors' =\u003e [\n            ['name' =\u003e 'username', 'error' =\u003e 'Username must be at least 5 characters long.'],\n            ['name' =\u003e 'email', 'error' =\u003e 'Provided address is not a valid email.'],\n        ],\n    ]\n\n    $apiProblem = ApiProblem::fromException(\n        $exception,\n        'Input values do not match the requirements',\n        'user.invalid_data',\n        $additionalDetails;\n    ); \n\n    $presenter = new JsonPresenter($apiProblem); //or XmlPresenter\n    \n    return new ApiProblemResponse($presenter);\n}\n```\n\n#### JSON Output\n \n**Headers**\n```\nHTTP/1.1 500 Bad Request\nContent-Type: application/problem+json\n```   \n\n**Body**\n```json\n{    \n    \"title\": \"Input values do not match the requirements\",\n    \"status\": 500,\n    \"detail\": \"User data is not valid.\",\n    \"type\": \"user.invalid_data\",\n    \"errors\": [\n        {\n            \"name\": \"username\",\n            \"error\": \"Username must be at least 5 characters long.\"\n        },\n        {\n            \"name\": \"email\",\n            \"error\": \"Provided address is not a valid email.\"\n        }        \n    ]\n}\n```\n\n#### XML Output\n \n**Headers**\n```\nHTTP/1.1 500 Bad Request\nContent-Type: application/problem+xml\n```   \n\n**Body**\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cproblem xmlns=\"urn:ietf:rfc:7807\"\u003e  \n  \u003ctitle\u003eInput values do not match the requirements\u003c/title\u003e\n  \u003cstatus\u003e500\u003c/status\u003e\n  \u003cdetail\u003eUser data is not valid.\u003c/detail\u003e\n  \u003ctype\u003euser.invalid_data\u003c/type\u003e\n  \u003cerrors\u003e\n    \u003citem\u003e\n      \u003cname\u003eusername\u003c/name\u003e\n      \u003cerror\u003eUsername must be at least 5 characters long.\u003c/error\u003e\n    \u003c/item\u003e\n    \u003citem\u003e\n      \u003cname\u003eemail\u003c/name\u003e\n      \u003cerror\u003eProvided address is not a valid email.\u003c/error\u003e\n    \u003c/item\u003e    \n  \u003c/errors\u003e\n\u003c/problem\u003e\n```\n\n---\n\n\n## Contribute\n\nContributions to the package are always welcome!\n\n* Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-api-problems/issues/new).\n* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/php-api-problems).\n\n## Support\n\nGet in touch with me using one of the following means:\n\n - Emailing me at \u003ccontact@nilportugues.com\u003e\n - Opening an [Issue](https://github.com/nilportugues/php-api-problems/issues/new)\n\n## Authors\n\n* [Nil Portugués Calderó](http://nilportugues.com)\n* [The Community Contributors](https://github.com/nilportugues/php-api-problems/graphs/contributors)\n\n\n## License\nThe code base is licensed under the [MIT license](LICENSE).\n","funding_links":["https://paypal.me/nilportugues"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-api-problems","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilportugues%2Fphp-api-problems","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-api-problems/lists"}