apsxj / json
JSON 1.0 Library for Web APIs
v0.1.1
2021-05-10 02:27 UTC
This package is auto-updated.
Last update: 2024-06-17 23:50:00 UTC
README
json
JSON 1.0 Library for Web APIs
This library was designed using the JSON:API v1.0 STABLE specification.
Getting Started
- Add the following to your
composer.json
file:
"require": { "apsxj/json": "dev-main" }, "repositories": [ { "type": "git", "url": "https://github.com/apsxj/json.git" } ]
-
Run
composer install
-
Before calling any of the methods, require the vendor autoloader
// For example, from the root directory... require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
- To create a
Response
object and render it:
<?php // unwrap the Response class from the apsxj\json namespace use apsxj\json\Response; // Create a new response $response = new Response(); // Add some data $response->appendData( array( 'method' => 'test', 'result' => 'success' ) ); // Render the response $response->render();
Output should look something like this:
{ "links": { "self": "https:\/\/localhost\/api\/test" }, "meta": { "timestamp": 1620587659, "object": "dictionary", "count": 1 }, "data": [ { "method": "test", "result": "success" } ] }
- To create an error response and render it:
<?php // unwrap the Response class from the apsxj\json namespace use apsxj\json\Response; // Create a new response $response = new Response(); // Add the error $response->appendError( 404, 'Not Found', 'The requested resource was not found' ); // Render the response $response->render();
Output should look something like this:
{ "errors": [ { "status": 404, "title": "Not Found", "detail": "The requested resource was not found" } ] }