{"id":36979764,"url":"https://github.com/harryosmar/php-restful-api-response","last_synced_at":"2026-01-13T22:49:22.688Z","repository":{"id":62514821,"uuid":"121479513","full_name":"harryosmar/php-restful-api-response","owner":"harryosmar","description":"php restful api response implement PSR-7: HTTP message interfaces","archived":false,"fork":false,"pushed_at":"2019-01-22T23:38:44.000Z","size":39,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-14T03:42:41.728Z","etag":null,"topics":["composer-plugin","php","psr7-http","response-code","rest-api","transformer"],"latest_commit_sha":null,"homepage":null,"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/harryosmar.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":"2018-02-14T06:40:58.000Z","updated_at":"2025-01-21T16:52:53.000Z","dependencies_parsed_at":"2022-11-02T10:17:18.612Z","dependency_job_id":null,"html_url":"https://github.com/harryosmar/php-restful-api-response","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/harryosmar/php-restful-api-response","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harryosmar%2Fphp-restful-api-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harryosmar%2Fphp-restful-api-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harryosmar%2Fphp-restful-api-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harryosmar%2Fphp-restful-api-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harryosmar","download_url":"https://codeload.github.com/harryosmar/php-restful-api-response/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harryosmar%2Fphp-restful-api-response/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28403750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["composer-plugin","php","psr7-http","response-code","rest-api","transformer"],"created_at":"2026-01-13T22:49:22.608Z","updated_at":"2026-01-13T22:49:22.675Z","avatar_url":"https://github.com/harryosmar.png","language":"PHP","readme":"# php rest-api response implement PSR-7: HTTP message interfaces\n\n[![Latest Version](https://img.shields.io/github/release/harryosmar/php-restful-api-response.svg?style=flat-square)](https://github.com/harryosmar/php-restful-api-response/releases)\n[![Build Status](https://travis-ci.org/harryosmar/php-restful-api-response.svg?branch=master)](https://travis-ci.org/harryosmar/php-restful-api-response)\n[![Build Status](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/badges/build.png?b=master)](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/build-status/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/?branch=master)\n\n## Requirements\n- php \u003e= 7.0\n- composer https://getcomposer.org/download/\n\n## Features\n- Implement PSR-7: HTTP message interfaces, extend https://github.com/zendframework/zend-diactoros\n- Provides response format [collection](#with-collection) \u0026 [item](#with-item) using library http://fractal.thephpleague.com/\n- Provides basic [errors response](#error)\n\n\n## How To Setup\n- *add this lines to your `composer.json` file*\n```\n{\n    \"require\": {\n        \"harryosmar/php-restful-api-response\": \"^1.1\"\n    }\n}\n```\n- *then run `composer install` or `composer update`*\n\n## How To Use\n\nSimple example how to use\n```\n\u003c?php\n\nuse PhpRestfulApiResponse\\Response;\n\n$response = new Response();\n\necho $response-\u003ewithArray([\n    'status' =\u003e 'created',\n    'id' =\u003e 1\n], 200); //response code 200\n```\nresponse\n```json\n{\n    \"status\": \"created\",\n    \"id\": 1\n}\n```\n\n## Available Response Format\n* [with array](#with-array)\n* [with item](#with-item)\n* [with collection](#with-collection)\n* [error](#error)\n    * [With Error](#with-error)\n    * [403 Forbidden](#403-forbidden)\n    * [500 Internal Server Error](#500-internal-server-error)\n    * [404 Not Found](#404-not-found)\n    * [401 Unauthorized](#401-unauthorized)\n    * [400 Bad Request](#400-bad-request)\n    * [410 Gone](#410-gone)\n    * [405 Method Not Allowed](#405-method-not-allowed)\n    * [431 Request Header Fields Too Large](#431-request-header-fields-too-large)\n    * [422 Unprocessable Entity](#422-unprocessable-entity)\n\n##### With Array\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003ewithArray([\n    'status' =\u003e 'created',\n    'id' =\u003e 1\n], 201); //response code 201\n```\nresponse\n```json\n{\n    \"status\": \"created\",\n    \"id\": 1\n}\n```\n\n##### With Item\nFor this sample, we use [class Book](https://github.com/harryosmar/php-restful-api-response/blob/master/tests/unit/Lib/Book.php) as an item\n```php\n\u003c?php\nuse PhpRestfulApiResponse\\Tests\\unit\\Lib\\Book;\n\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003ewithItem(\n    new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),\n    new \\PhpRestfulApiResponse\\Tests\\unit\\Lib\\Transformer\\Book,\n    201\n);\n```\nresponse 201\n```json\n{\n    \"data\":\n    {\n        \"title\": \"how to be a ninja\",\n        \"author\":\n        {\n            \"name\": \"harry\",\n            \"email\": \"harryosmarsitohang@gmail.com\"\n        },\n        \"year\": 2017,\n        \"price\": 100000\n    }\n}\n```\n\n##### With Collection\n```php\n\u003c?php\nuse PhpRestfulApiResponse\\Tests\\unit\\Lib\\Book;\n\n/** @var \\PhpRestfulApiResponse\\Response $response */\n$response-\u003ewithCollection(\n    [\n        new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),\n        new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a mage', 500000, 2016),\n        new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a samurai', 25000, 2000),\n    ],\n    new \\PhpRestfulApiResponse\\Tests\\unit\\Lib\\Transformer\\Book,\n    200\n);\n```\nresponse 200\n```json\n{\n    \"data\": [\n    {\n        \"title\": \"how to be a ninja\",\n        \"author\":\n        {\n            \"name\": \"harry\",\n            \"email\": \"harryosmarsitohang@gmail.com\"\n        },\n        \"year\": 2017,\n        \"price\": 100000\n    },\n    {\n        \"title\": \"how to be a mage\",\n        \"author\":\n        {\n            \"name\": \"harry\",\n            \"email\": \"harryosmarsitohang@gmail.com\"\n        },\n        \"year\": 2016,\n        \"price\": 500000\n    },\n    {\n        \"title\": \"how to be a samurai\",\n        \"author\":\n        {\n            \"name\": \"harry\",\n            \"email\": \"harryosmarsitohang@gmail.com\"\n        },\n        \"year\": 2000,\n        \"price\": 25000\n    }]\n}\n```\n\n#### Error\n\n##### With Error\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003ewithError(['error' =\u003e 'something is wrong, please try again'], 500);\n```\nresponse 500\n```json\n{\n    \"error\": \"something is wrong, please try again\"\n}\n```\n\n##### 403 Forbidden\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorNotFound();\n```\nresponse 403\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 403,\n        \"phrase\": \"Forbidden\"\n    }\n}\n```\n\n##### 500 Internal Server Error\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorInternalError();\n```\nresponse 500\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 500,\n        \"phrase\": \"Internal Server Error\"\n    }\n}\n```\n\n##### 404 Not Found\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorNotFound();\n```\nresponse 404\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 404,\n        \"phrase\": \"Not Found\"\n    }\n}\n```\n\n##### 401 Unauthorized\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorUnauthorized();\n```\nresponse 401\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 401,\n        \"phrase\": \"Unauthorized\"\n    }\n}\n```\n\n##### 400 Bad Request\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorWrongArgs([\n   'username' =\u003e 'required',\n   'password' =\u003e 'required'\n]);\n```\nresponse 400\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 400,\n        \"phrase\": \"Bad Request\",\n        \"message\":\n        {\n            \"username\": \"required\",\n            \"password\": \"required\"\n        }\n    }\n}\n```\n\n##### 410 Gone\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorGone();\n```\nresponse 410\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 410,\n        \"phrase\": \"Gone\"\n    }\n}\n```\n\n##### 405 Method Not Allowed\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorMethodNotAllowed();\n```\nresponse 405\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 405,\n        \"phrase\": \"Method Not Allowed\"\n    }\n}\n```\n\n##### 431 Request Header Fields Too Large\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorUnwillingToProcess();\n```\nresponse 431\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 431,\n        \"phrase\": \"Request Header Fields Too Large\"\n    }\n}\n```\n\n##### 422 Unprocessable Entity\n```php\n\u003c?php\n/** @var \\PhpRestfulApiResponse\\Response $response */\necho $response-\u003eerrorUnprocessable();\n```\nresponse 422\n```json\n{\n    \"error\":\n    {\n        \"http_code\": 422,\n        \"phrase\": \"Unprocessable Entity\"\n    }\n}\n```\n\n## How To Run The Test\n```\ncomposer test\n```\n\n## How To Contribute\n- Fork this repo\n- post an issue https://github.com/harryosmar/php-restful-api-response/issues\n- create the PR(Pull Request) and wait for the review\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharryosmar%2Fphp-restful-api-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharryosmar%2Fphp-restful-api-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharryosmar%2Fphp-restful-api-response/lists"}