PHP code example of soc-warden / php-sdk

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

    

soc-warden / php-sdk example snippets


use SOCWarden\SOCWarden;

$soc = new SOCWarden(
    apiKey:   'your-api-key',
    endpoint: 'https://ingestor.socwarden.com',
);

// Simple tracking with named arguments
$soc->track('auth.login.success', actorId: 'usr_123', actorEmail: '[email protected]');

// Track with metadata
$soc->track('data.exported', actorId: 'usr_456', metadata: ['format' => 'csv', 'rows' => 1500]);

$soc->event('auth.login.failure')
    ->actorEmail('[email protected]')
    ->ip('203.0.113.42')
    ->meta('reason', 'invalid_password')
    ->meta('attempt', 3)
    ->send();

$soc->event('data.exported')
    ->actor('usr_123', '[email protected]')
    ->resource('Report', 42)
    ->metadata(['format' => 'csv', 'rows' => 1500])
    ->timestamp(new \DateTimeImmutable())
    ->severity('medium')
    ->send();

$soc->trackData('auth.login.success', [
    'actor_id'    => 'usr_123',
    'actor_email' => '[email protected]',
    'ip'          => '203.0.113.42',
    'user_agent'  => 'Mozilla/5.0 ...',
    'metadata'    => ['role' => 'admin'],
]);

use SOCWarden\SOCWarden;
use SOCWarden\Middleware\PSR15Middleware;
use Slim\Factory\AppFactory;

$soc = new SOCWarden(
    apiKey:   'your-api-key',
    endpoint: 'https://ingestor.socwarden.com',
);

$app = AppFactory::create();
$app->add(new PSR15Middleware($soc));

$app->post('/login', function ($request, $response) use ($soc) {
    // Request context is automatically captured
    $soc->track('auth.login.success', actorId: $userId);
    // ...
});

// config/pipeline.php
$app->pipe(new \SOCWarden\Middleware\PSR15Middleware($container->get(\SOCWarden\SOCWarden::class)));

// Any PSR-7 ServerRequestInterface
$soc->setRequest($psrRequest);

// Now all tracked events 
bash
composer