PHP code example of snappmarket / api-responder

1. Go to this page and download the library: Download snappmarket/api-responder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snappmarket / api-responder example snippets


ApiResponderErrors::register(
     '40001',
     'Verbose, plain language description of the problem. Provide developers suggestions about how to solve their problems here',
     'This is a message that can be passed along to end-users, if needed.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

ApiResponderErrors::unregister('40001');

    ApiResponderResponse::clientError('40001');
    

    ApiResponderResponse::serverError('40001');
    

ApiResponderResponse::error('40001', 422);

ApiResponderResponse::register(
     '40001',
     'The class `:class` is undefined.',
     'An error has been occurred in while finding the :entity.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

return ApiResponderResponse::clientError(40001, [
     'class'  => 'Entities/User',
     'entity' => 'user',
],
[
     'field' => [
         'The field has an error.'
     ]
])

ApiResponderErrors::disableExceptions();

$results  = [
     [
          'id'    => 1,
          'title' => 'First Item',
     ],
     [
          'id'    => 2,
          'title' => 'Second Item',
     ],
];
$metadata = ['page' => 1];
$messages = [
    'The list of the items may be incomplete in some cases'
];
    
$response = ApiResponderResponse::success($results, $metadata, $messages);

ApiResponderErrors::register(
     '40001',
     'Verbose, plain language description of the problem. Provide developers suggestions about how to solve their problems here',
     'This is a message that can be passed along to end-users, if needed.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

ApiResponderErrors::registerFormatter(function ($key) {
    return snake_case($key);
});


return ApiResponderErrors::clientError(40001);