PHP code example of saas-cb / spr-php-client

1. Go to this page and download the library: Download saas-cb/spr-php-client 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/ */

    

saas-cb / spr-php-client example snippets


use SaasCb\SprClient\Client;
use SaasCb\SprClient\Configuration;

$config = new Configuration('https://gateway.example.com', 'your-api-key');
$client = new Client($config);

$config = new Configuration('https://gateway.example.com');
$client = new Client($config);

$ping = $client->ping();
if ($ping->isSpr()) {
    echo "Gateway и SPR доступны\n";
}

// Список деревьев
$response = $client->listTrees();
foreach ($response->getData()->getItems() as $tree) {
    echo $tree->getName() . "\n";
}

// Список деревьев с фильтрацией, сортировкой и пагинацией
$filters = [
    'name' => 'test',
    'page' => 1,
    'limit' => 20,
    'sort' => 'name',
    'order' => 'asc',
];
$response = $client->withQueryParameters(array_filter($filters))->listTrees();
$data = $response->getData();
echo "Страница {$data->getPage()} из " . ceil($data->getTotal() / $data->getLimit()) . "\n";

// Получить дерево
$tree = $client->getTree('tree-uuid');

// Создать дерево
$tree = $client->createTree(new CreateTreeRequest(
    name: 'My tree',
    config: ['nodes' => [], 'edges' => []],
));

// Обновить дерево
$tree = $client->updateTree('tree-uuid', new UpdateTreeRequest(name: 'Updated'));

// Удалить дерево
$client->deleteTree('tree-uuid');

use SaasCb\SprClient\Request\EvaluateRequest;

$request = (new EvaluateRequest(customerId: 123, applicationId: 'app-456'))
    ->addContextField('birth_date', '1990-01-01')
    ->addContextField('passport', '1234567890');

$reference = $client->evaluateTree('tree-uuid', $request);
echo $reference->getRunUuid();

$status = $client->getRunStatus('run-uuid');
$result = $client->getRunResult('run-uuid');

$response = $client->listScorings();
foreach ($response->getItems() as $scoring) {
    echo $scoring->getCode() . ': ' . $scoring->getName() . "\n";
}

// С фильтром по категории
$response = $client->listScorings('basic');

$inputs = $client->getTreeInputs('tree-uuid');
foreach ($inputs as $name => $scoringInput) {
    echo $name . ': ' . $scoringInput->getCode() . "\n";
}

use SaasCb\SprClient\Exception\AuthenticationException;
use SaasCb\SprClient\Exception\NotFoundException;
use SaasCb\SprClient\Exception\ValidationException;
use SaasCb\SprClient\Exception\ServerException;

try {
    $client->getTree('unknown-uuid');
} catch (NotFoundException $e) {
    echo "Не найдено (HTTP {$e->getHttpStatusCode()}): {$e->getMessage()}\n";
} catch (AuthenticationException $e) {
    echo "Ошибка авторизации\n";
} catch (ValidationException $e) {
    echo "Ошибка валидации: {$e->getMessage()}\n";
} catch (ServerException $e) {
    echo "Ошибка сервера: {$e->getMessage()}\n";
}