A simple PHP API helper to generate JSON or XML result
composer require pingcheng/api-response| Format | Support | 
|---|---|
| Json | ✅ | 
| XML | ✅ | 
use PingCheng\ApiResponse\ApiResponse;
echo ApiResponse::json()
    ->code(400)
    ->addProperty('message', [
        'your result has been accepted'
    ])
    ->data([
        'id' => '1234',
        'result' => 'accepted'
    ]);Or
ApiResponse::json()
    ->code(400)
    ->addProperty('message', [
        'your result has been accepted'
    ])
    ->data([
        'id' => '1234',
        'result' => 'accepted'
    ])
    ->send();
// The program will stop by send() and return the result instantlyThe reuslt you would have
{
  "code": 400,
  "status": "Bad Request",
  "data": {
    "id": "1234",
    "result": "accepted"
  },
  "message": [
    "your result has been accepted"
  ]
}Or, if you want to have a XML result
use PingCheng\ApiResponse\ApiResponse;
echo ApiResponse::xml()
    ->code(400)
    ->addProperty('message', [
        'your result has been accepted'
    ])
    ->data([
        'id' => '1234',
        'result' => 'accepted'
    ]);the result you would have
<root>
  <code>400</code>
  <status>Bad Request</status>
  <data>
    <id>1234</id>
    <result>accepted</result>
  </data>
  <message>your result has been accepted</message>
</root>- addProperty(name, value);
- removeProperty(name);
Basically, 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
// for modify 
...
->status('the new status message')
...
    
// for remove it
...
->removeProperty('status')
...- addHeader(name, value);
- removeHeader(name);
- removeAllHeaders();
You can use short cuts to specific a status code
ApiResponose::json()->success()->send();