Skip to content

pingcheng/api-response-archive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api-response

Build Status Coverage Status

A simple PHP API helper to generate JSON or XML result

Installation

composer require pingcheng/api-response

Supproted format

Format Support
Json
XML

Usage

Basic

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 instantly

The 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>

Property

  • 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') ...

Headers control

  • addHeader(name, value);
  • removeHeader(name);
  • removeAllHeaders();

Status shortcuts

You can use short cuts to specific a status code

ApiResponose::json()->success()->send();

About

A simple PHP API helper to generate JSON or XML result

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages