PHP code example of anothy / slim-api-wrapper

1. Go to this page and download the library: Download anothy/slim-api-wrapper 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/ */

    

anothy / slim-api-wrapper example snippets


$container['slim-api-wrapper'] = function (\Slim\Container $c) {
    return new \Anothy\SlimApiWrapper($c);
};

$app = new \Slim\App();

$app->get('/books', function ($request, $response, $args) {
    // Show all books
})->setName('api-books');

$slimApiWrapper = $app->getContainer()->get('slim-api-wrapper');

$result = $slimApiWrapper->call('GET', 'api-books', [
   'queryParams' => [
       'page' => 1,
   ],
]);

$result = $slimApiWrapper->callMiddlewareStack('GET', 'api-books', [
    'queryParams' => [
        'page' => 1,
    ],
]);

$app->get('/books/{id:[0-9]+}', function ($request, $response, $args) {
    // Show all books by `id`
})->setName('api-books-by-id');

$result = $slimApiWrapper->call('GET', 'api-books-by-id', [
    'namedArgs' => [
        'id' => 1234,
    ],
]);

$result = $slimApiWrapper->call('GET', 'api-books', [
    'headers' => [
        'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
    ],
    'queryParams' => [
        'page' => 1,
    ],
]);

$result = $slimApiWrapper->call('POST', 'api-books-post', [
    'headers' => [
        'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
    ],
    'payload' => [
        'name'   => 'The Name of the Wind',
        'author' => 'Patrick Rothfuss',
    ],
]);