Simple and friendly library for api servers (PHP serving out JSON).
It generates json output according to the jsonapi.org standard, but aims to be easy to understand for people without knowledge of the jsonapi standard.
A small example:
use alsvanzelf\jsonapi;
$user = new stdClass();
$user->id = 42;
$user->name = 'Zaphod Beeblebrox';
$user->heads = 2;
$jsonapi = new jsonapi\resource($type='user', $user->id);
$jsonapi->fill_data($user);
$jsonapi->send_response();
Which will result in:
{
"links": {
"self": "/examples/resource.php"
},
"data": {
"type": "user",
"id": 42,
"attributes": {
"name": "Zaphod Beeblebrox",
"heads": 2
},
"links": {
"self": "/examples/resource.php"
}
}
}
Examples for all kind of responses are in the /examples directory.
Use Composer. And use require to get the latest stable version:
composer require alsvanzelf/jsonapi
Right now, this library handles all the basics:
- generating single resources
- generating resource collections
- handling error responses
- sending out the json response with correct http headers
Plans for the near and later future include:
- import a database array as a collection response (#2)
- accept a collection as to-many relation in a resource (#3)
- sending out redirect locations and status codes for non-error responses (#4)
- handle creating, updating and deleting resources (#5)
Pull Requests or issues are welcome!