1. Go to this page and download the library: Download racoon/api 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/ */
racoon / api example snippets
// Create an instance of Racoon
$app = new Racoon\Api\App();
$router = $app->getRouter();
$router->addRouteFile('/path/to/my_routes.php');
$app->run();
$formatter = new \Racoon\Api\Response\Format\JsonFormatter();
$app->setResponseFormatter($formatter);
throw new \Racoon\Api\Exception(
Request $request = null, // Not the response. False if the framework shouldn't handle it.
$message, // Your error message.
$code = 0, // If this is not 0 and $displayAsError is True, the http response code will be set to this.
\Exception $previous = null // If this is as a result of a previous Exception you can pass that in here.
);
namespace Racoon\Api\Exception;
use Racoon\Api\Request;
class AuthenticationException extends Exception
{
public function __construct(Request $request = null, $message, \Exception $previous = null)
{
parent::__construct($request, true, $message, 401, $previous);
}
}
throw new \Racoon\Api\AuthenticationException(null, 'Missing API Key.');
$authenticator = new \Racoon\Api\Auth\ApiKeyAuthenticator();
$app->setAuthenticator($authenticator);
$authenticator = new \Racoon\Api\Auth\NoAuthenticator();
$app->setAuthenticator($authenticator);
$authenticator = new \Racoon\Api\Auth\ApiKeyAuthenticator();
$authenticator->setApiKeyName('api_key'); // Tells it to look under api_key to find the api key.
$authenticator->addValidApiKey('dsdasdasdasd'); // Add a valid API key.
$app->setAuthenticator($authenticator);
use Racoon\Api\Schema\Schema;
use Racoon\Api\Schema\Translator;
$schema = Schema::create([
Translator::item('username')->isString(2, 20)->returnItem(),
Translator::item('password')->isString(6)->returnItem(),
]);
$this->validateSchema($schema);
use Racoon\Api\Schema\Schema;
use Racoon\Api\Schema\Translator;
$schema = Schema::create([
Translator::item('username')->isString(2, 4)->alt()->isString(10, 12)->returnItem(),
]);
$this->validateSchema($schema);
namespace MyApp;
class Request extends \Racoon\Api\Request
{
}
$app->setRequestClass('\\MyApp\\Request');
// Fetches the username from the input data, and if it doesn't exist it will return 'unknown'.
$request->getOptionalRequestData('username', 'unknown');
// Fetches the username from the input data.
// If it doesn't exist or has a length of 0, an error message will be shown to the client.
$request->getRequiredRequestData('username');