PHP code example of mauricioperera / php-a2e

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

    

mauricioperera / php-a2e example snippets


use PHPA2E\A2E;
use PHPA2E\Config;

$a2e = new A2E(new Config(masterKey: 'your-secret-key'));

// Define a workflow in JSONL
$workflow = implode("\n", [
    json_encode(['type' => 'operationUpdate', 'operationId' => 'fetch-users',
        'operation' => ['ApiCall' => [
            'method' => 'GET',
            'url' => 'https://jsonplaceholder.typicode.com/users',
            'outputPath' => '/workflow/users',
        ]]]),
    json_encode(['type' => 'operationUpdate', 'operationId' => 'filter-active',
        'operation' => ['FilterData' => [
            'inputPath' => '/workflow/users',
            'conditions' => [['field' => 'id', 'operator' => '<=', 'value' => 5]],
            'outputPath' => '/workflow/filtered',
        ]]]),
    json_encode(['type' => 'beginExecution', 'executionId' => 'exec-1',
        'operationOrder' => ['fetch-users', 'filter-active']]),
]);

// Validate
$validation = $a2e->validate($workflow);
// { valid: true, errors: 0, warnings: 0, issues: [] }

// Execute
$result = $a2e->execute($workflow);
// { status: "success", results: { "fetch-users": {...}, "filter-active": {count: 5} } }

$result = $a2e->validate($jsonl);
// Stage 1: Structure    — IDs exist, no duplicates,     — FilterData needs array input, etc.
// Stage 4: API compat   — URLs registered (optional)
// Stage 5: Credentials  — refs exist in vault (optional)
// Stage 6: Patterns     — infinite loops, large workflows

// Register an agent with permissions
$apiKey = $a2e->auth->register('agent-1', 'My Agent',
    allowedApis: ['user-api'],
    allowedCredentials: ['api-token'],
    allowedOperations: ['ApiCall', 'FilterData'],
);

// Agent authenticates with API key
$agentId = $a2e->auth->authenticate($apiKey);

// Capabilities filtered by permissions
$caps = $a2e->capabilities('agent-1');

// Store encrypted credential
$a2e->vault->store('api-token', 'bearer-token', 'secret-value', ['api' => 'users']);

// In workflows, agents reference by ID only
// {"Authorization": {"credentialRef": {"id": "api-token"}}}
// → Server injects: {"Authorization": "Bearer secret-value"}

use PHPA2E\Operation\OperationInterface;
use PHPA2E\Executor\DataModel;

class SendSlackMessage implements OperationInterface
{
    public function type(): string { return 'SendSlack'; }

    public function execute(array $config, DataModel $data): array
    {
        $message = $data->resolveReferences($config['message']);
        // ... send to Slack
        return ['sent' => true, 'channel' => $config['channel']];
    }
}

$a2e->operations->register(new SendSlackMessage());

composer 
bash
php -S 0.0.0.0:3210 bin/server.php -- --master-key SECRET --api-key AUTH