{"id":18457448,"url":"https://github.com/pingcheng/api-response-archive","last_synced_at":"2025-04-22T23:17:51.447Z","repository":{"id":116906407,"uuid":"127144403","full_name":"pingcheng/api-response-archive","owner":"pingcheng","description":"A simple PHP API helper to generate JSON or XML result","archived":false,"fork":false,"pushed_at":"2018-08-31T10:49:16.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T23:17:46.809Z","etag":null,"topics":["api","api-helper","php-api","restful","restful-api"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pingcheng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-03-28T13:28:19.000Z","updated_at":"2024-04-15T07:46:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"3ac2d660-74f0-4852-aaf1-88532b901151","html_url":"https://github.com/pingcheng/api-response-archive","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcheng%2Fapi-response-archive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcheng%2Fapi-response-archive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcheng%2Fapi-response-archive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcheng%2Fapi-response-archive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pingcheng","download_url":"https://codeload.github.com/pingcheng/api-response-archive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250337957,"owners_count":21414104,"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":["api","api-helper","php-api","restful","restful-api"],"created_at":"2024-11-06T08:14:24.640Z","updated_at":"2025-04-22T23:17:51.426Z","avatar_url":"https://github.com/pingcheng.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# api-response\n[![Build Status](https://travis-ci.org/pingcheng/api-response.svg?branch=master)](https://travis-ci.org/pingcheng/api-response)\n[![Coverage Status](https://coveralls.io/repos/github/pingcheng/api-response/badge.svg?branch=master)](https://coveralls.io/github/pingcheng/api-response?branch=master)\n\nA simple PHP API helper to generate JSON or XML result\n\n# Installation\n```bash\ncomposer require pingcheng/api-response\n```\n\n# Supproted format\n| Format  | Support |\n| ------------- | ------------- |\n| Json  | ✅  |\n| XML  | ✅  |\n\n# Usage\n\n## Basic\n```php\nuse PingCheng\\ApiResponse\\ApiResponse;\n​\necho ApiResponse::json()\n    -\u003ecode(400)\n    -\u003eaddProperty('message', [\n        'your result has been accepted'\n    ])\n    -\u003edata([\n        'id' =\u003e '1234',\n        'result' =\u003e 'accepted'\n    ]);\n```\n\nOr\n```php\nApiResponse::json()\n    -\u003ecode(400)\n    -\u003eaddProperty('message', [\n        'your result has been accepted'\n    ])\n    -\u003edata([\n        'id' =\u003e '1234',\n        'result' =\u003e 'accepted'\n    ])\n    -\u003esend();\n// The program will stop by send() and return the result instantly\n```\nThe reuslt you would have\n```json\n{\n  \"code\": 400,\n  \"status\": \"Bad Request\",\n  \"data\": {\n    \"id\": \"1234\",\n    \"result\": \"accepted\"\n  },\n  \"message\": [\n    \"your result has been accepted\"\n  ]\n}\n```\n\nOr, if you want to have a XML result\n```php\nuse PingCheng\\ApiResponse\\ApiResponse;\n​\necho ApiResponse::xml()\n    -\u003ecode(400)\n    -\u003eaddProperty('message', [\n        'your result has been accepted'\n    ])\n    -\u003edata([\n        'id' =\u003e '1234',\n        'result' =\u003e 'accepted'\n    ]);\n```\n\nthe result you would have\n```xml\n\u003croot\u003e\n  \u003ccode\u003e400\u003c/code\u003e\n  \u003cstatus\u003eBad Request\u003c/status\u003e\n  \u003cdata\u003e\n    \u003cid\u003e1234\u003c/id\u003e\n    \u003cresult\u003eaccepted\u003c/result\u003e\n  \u003c/data\u003e\n  \u003cmessage\u003eyour result has been accepted\u003c/message\u003e\n\u003c/root\u003e\n```\n\n## Property\n- addProperty(name, value);\n- removeProperty(name);\n\nBasically, ApiReponse would auto add a status description to the result based on the status code, for example, if you add 200 code to the response, the api would automatically add a status of \"OK!\". If you need to modify it, please use this function to add it or remove it\n```php\n// for modify \n...\n-\u003estatus('the new status message')\n...\n    \n// for remove it\n...\n-\u003eremoveProperty('status')\n...\n```\n\n## Headers control\n- addHeader(name, value);\n- removeHeader(name);\n- removeAllHeaders();\n\n## Status shortcuts\nYou can use short cuts to specific a status code\n```php\nApiResponose::json()-\u003esuccess()-\u003esend();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpingcheng%2Fapi-response-archive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpingcheng%2Fapi-response-archive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpingcheng%2Fapi-response-archive/lists"}