PHP code example of apirelio / flightphp

1. Go to this page and download the library: Download apirelio/flightphp 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/ */

    

apirelio / flightphp example snippets


use Apirelio\FlightPHP\ApirelioMiddleware;
use Apirelio\FlightPHP\Config as ApirelioConfig;
use flight\Engine;

$app = new Engine;
$apirelio = new ApirelioMiddleware($app, new ApirelioConfig(
    apiKey: (string) getenv('APIRELIO_API_KEY'),
    service: 'billing-api',
    environment: getenv('APP_ENV') ?: 'production',
    release: getenv('APP_RELEASE') ?: null,
    paths: ['/api/*'],
    bufferPath: __DIR__.'/../storage/apirelio-events.ndjson',
));

$app->group('/api', function () use ($app): void {
    $app->get('/invoices/@id', [InvoiceController::class, 'show'], false, 'invoices:view');
}, [$apirelio]);

$app->start();

use Apirelio\Core\Data\ApirelioApplication;
use Apirelio\Core\Data\ApirelioCustomer;

$apirelio = new ApirelioMiddleware(
    app: $app,
    config: $config,
    customerResolver: static function (Engine $app, array $params): ?ApirelioCustomer {
        $account = $app->account();

        return $account === null ? null : new ApirelioCustomer(
            id: (string) $account->id,
            name: $account->name,
            plan: $account->plan,
        );
    },
    applicationResolver: static fn (Engine $app): ApirelioApplication => new ApirelioApplication(
        id: (string) $app->apiClient()->id,
    ),
);

$apirelio->context()?->addMetadata(['region' => 'eu-central']);
$apirelio->context()?->setErrorCode('PAYMENT_REQUIRED');