{"id":18498481,"url":"https://github.com/zircote/apiproblem","last_synced_at":"2025-04-09T00:31:19.592Z","repository":{"id":9039185,"uuid":"10801586","full_name":"zircote/ApiProblem","owner":"zircote","description":"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).","archived":false,"fork":false,"pushed_at":"2013-12-16T13:57:23.000Z","size":133,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-23T19:45:14.764Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zircote.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-2.0","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-19T17:42:23.000Z","updated_at":"2024-11-28T15:55:47.000Z","dependencies_parsed_at":"2022-09-13T06:50:17.155Z","dependency_job_id":null,"html_url":"https://github.com/zircote/ApiProblem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zircote%2FApiProblem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zircote%2FApiProblem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zircote%2FApiProblem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zircote%2FApiProblem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zircote","download_url":"https://codeload.github.com/zircote/ApiProblem/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247949889,"owners_count":21023410,"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","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":[],"created_at":"2024-11-06T13:40:32.285Z","updated_at":"2025-04-09T00:31:18.696Z","avatar_url":"https://github.com/zircote.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ApiProblem\n\n\n - [![Master Build Status](https://secure.travis-ci.org/zircote/ApiProblem.png?branch=master)](http://travis-ci.org/zircote/ApiProblem) `master`\n\n\n`ApiProblem` is an attempt to provide the functionality and problem reporting as defined in \n[Problem Details for HTTP APIs](http://tools.ietf.org/html/draft-nottingham-http-problem). The goal being a simple \nException wrapper for PHP that can send the desired response in `JSON` and `XML`.\n\n## Use\n\n#### The `sendHttpResponse` method\n\nThe `sendHTTPResponse` has two parameter, both optional `$format` and `$terminate`.\n - `format`: \n  - `ApiProblem::FORMAT_XML`: `application/api-problem+json`\n  - `ApiProblem::FORMAT_JSON`: `application/api-problem+xml`\n - `terminate`: `bool` \n  - `true` (default) headers are sent and execution is terminated.\n  - `false` the body payload is returned\n\n#### JSON Example\n\n```php\n\u003c?php\n$apiProblem = new ApiProblem(\n    'http://api-problem.domain.com/some-url.html',\n    'Bad Request',\n    400,\n    'some detail',\n    'http://domain.com/this-request'\n);\n$apiProblem-\u003esendHTTPResponse(ApiProblem::FORMAT_JSON);\n\n```\n\n#### Result\n\n```http\nHTTP/1.0 400 Bad Request\nAccess-Control-Allow-Origin: *\nContent-Type: application/api-problem+json\nLink: \u003chttp://api-problem.domain.com/some-url.html\u003e; rel=\"http://api-problem.domain.com/some-url.html\" title=\"Bad Request\"\n\n{\n    \"problemType\": \"http://api-problem.domain.com/some-url.html\",\n    \"title\": \"Bad Request\",\n    \"httpStatus\": 400,\n    \"detail\": \"some detail\",\n    \"problemInstance\": \"http://domain.com/this-request\"\n}\n\n```\n\n\n#### XML Example\n\n```php\n\u003c?php\ntry {\n  throw new ApiProblem(\n      'http://api-problem.domain.com/some-url.html',\n      'Bad Request',\n      400,\n      'some detail',\n      'http://domain.com/this-request'\n  );\n} catch (ApiProblem $e) {\n  $e-\u003esendHTTPResponse(ApiProblem::FORMAT_XML);\n}\n\n```\n\n#### Result\n\n```http\nHTTP/1.0 400 Bad Request\nAccess-Control-Allow-Origin: *\nContent-Type: application/api-problem+xml\nLink: \u003chttp://api-problem.domain.com/some-url.html\u003e; rel=\"http://api-problem.domain.com/some-url.html\"; title=\"Bad Request\"\n\n\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cproblem xmlns=\"urn:ietf:draft:nottingham-http-problem\"\u003e\n    \u003cproblemType\u003ehttp://api-problem.domain.com/some-url.html\u003c/problemType\u003e\n    \u003ctitle\u003eBad Request\u003c/title\u003e\n    \u003chttpStatus\u003e400\u003c/httpStatus\u003e\n    \u003cdetail\u003esome detail\u003c/detail\u003e\n    \u003cproblemInstance\u003ehttp://domain.com/this-request\u003c/problemInstance\u003e\n\u003c/problem\u003e\n\n```\n\n#### Adding additional headers, i.e. CORS, Auth, etc\n\n```php\n\u003c?php\n$apiProblem = new ApiProblem(\n    'http://api-problem.domain.com/some-url.html',\n    'Unauthorized',\n    401,\n    'some detail',\n    'http://domain.com/this-request'\n);\n\n$apiProblem-\u003esetHeader('Access-Control-Allow-Origin', '*');\n$apiProblem-\u003esetHeader('WWW-Authenticate', 'bearer realm=\"my_realm\"');\n$apiProblem-\u003esendHTTPResponse(ApiProblem::FORMAT_JSON);\n\n```\n\n#### Result\n\n```http\nHTTP/1.0 401 Unauthorized\nAccess-Control-Allow-Origin: *\nWWW-Authenticate: bearer realm=\"my_realm\"\nContent-Type: application/api-problem+json\nLink: \u003chttp://api-problem.domain.com/some-url.html\u003e; rel=\"http://api-problem.domain.com/some-url.html\" title=\"Bad Request\"\n\n{\n    \"problemType\": \"http://api-problem.domain.com/some-url.html\",\n    \"title\": \"Unauthorized\",\n    \"httpStatus\": 401,\n    \"detail\": \"some detail\",\n    \"problemInstance\": \"http://domain.com/this-request\"\n}\n\n```\n\n#### Adding extended data to the problem instance:\n\n```php\n\u003c?php\n$apiProblem = new ApiProblem(\n    'http://api-problem.domain.com/some-url.html',\n    'Unauthorized',\n    401,\n    'some detail',\n    'http://domain.com/this-request'\n);\n\n\n$apiProblem-\u003esetExtensionData('ext_test', 'etx_test_value');\n$apiProblem-\u003esetExtensionData('ext_test_array_i', array('a' =\u003e 'a_d', 'b' =\u003e 'b_d', 'c' =\u003e 'c_d'));\n$apiProblem-\u003esetExtensionData('ext_test_array_ni', array('a', 'b', 'c'));\n$apiProblem-\u003esendHTTPResponse(ApiProblem::FORMAT_JSON);\n\n```\n\n#### Result\n\n```http\nHTTP/1.0 401 Unauthorized\nContent-Type: application/api-problem+json\nLink: \u003chttp://api-problem.domain.com/some-url.html\u003e; rel=\"http://api-problem.domain.com/some-url.html\" title=\"Bad Request\"\n\n{\n    \"problemType\": \"http://api-problem.domain.com/some-url.html\",\n    \"title\": \"Unauthorized\",\n    \"httpStatus\": 401,\n    \"detail\": \"some detail\",\n    \"problemInstance\": \"http://domain.com/this-request\",\n    \"ext_test\":\"etx_test_value\",\n    \"ext_test_array_i\":{\n        \"a\":\"a_d\",\n        \"b\":\"b_d\",\n        \"c\":\"c_d\"\n    }\n    \"ext_test_array_ni\":[\n        \"a\",\n        \"b\",\n        \"c\"\n    ]\n}\n\n```\n[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/zircote/apiproblem/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzircote%2Fapiproblem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzircote%2Fapiproblem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzircote%2Fapiproblem/lists"}