PHP code example of harryosmar / php-restful-api-response

1. Go to this page and download the library: Download harryosmar/php-restful-api-response 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/ */

    

harryosmar / php-restful-api-response example snippets



/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 201); //response code 201


use PhpRestfulApiResponse\Tests\unit\Lib\Book;

/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withItem(
    new Book('harry', '[email protected]', 'how to be a ninja', 100000, 2017),
    new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
    201
);


use PhpRestfulApiResponse\Tests\unit\Lib\Book;

/** @var \PhpRestfulApiResponse\Response $response */
$response->withCollection(
    [
        new Book('harry', '[email protected]', 'how to be a ninja', 100000, 2017),
        new Book('harry', '[email protected]', 'how to be a mage', 500000, 2016),
        new Book('harry', '[email protected]', 'how to be a samurai', 25000, 2000),
    ],
    new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
    200
);


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withError(['error' => 'something is wrong, please try again'], 500);


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorInternalError();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnauthorized();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorWrongArgs([
   'username' => '


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorGone();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorMethodNotAllowed();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnwillingToProcess();


/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnprocessable();



use PhpRestfulApiResponse\Response;

$response = new Response();

echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 200); //response code 200