PHP code example of defenso / sdk-php

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

    

defenso / sdk-php example snippets


use Defenso\Client;

$this->app->singleton(Client::class, fn () => new Client(config('services.defenso.token')));

->withMiddleware(function ($middleware) {
    $middleware->append(\Defenso\Middleware\DefensoLaravelMiddleware::class);
})

'defenso' => [
    'token' => env('DEFENSO_TOKEN'),
],



$client = new \Defenso\Client(getenv('DEFENSO_TOKEN'));
$verdict = $client->inspect([
    'method' => $_SERVER['REQUEST_METHOD'],
    'url' => 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
    'headers' => getallheaders() ?: [],
    'body' => file_get_contents('php://input') ?: '',
    'ip' => $_SERVER['REMOTE_ADDR'] ?? null,
]);

if ($verdict['action'] === 'block') {
    http_response_code(403);
    echo json_encode(['blocked' => true, 'reason' => $verdict['reason'] ?? null]);
    exit;
}

new Defenso\Client(
    token: 'df_live_...',
    api: 'https://app.defen.so/api',    // override for self-hosted
    policyRefreshSeconds: 300,           // pull rules every 5 min
    logBatchSize: 50,                    // flush at this many events
    policyTimeoutSeconds: 1.0,           // fail-open threshold on policy fetch
);
bash
composer