PHP code example of iroute / sdk
1. Go to this page and download the library: Download iroute/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/ */
iroute / sdk example snippets
declare(strict_types=1);
use IRoute\IRouteClient;
RL') ?: 'http://localhost:8080',
token: getenv('IROUTE_TOKEN') ?: null,
tenantId: getenv('IROUTE_TENANT') ?: 'demo',
actorId: getenv('IROUTE_ACTOR') ?: 'php-app',
permissionScopes: [],
timeoutSeconds: 30,
);
$idempotencyKey = 'php-email-' . bin2hex(random_bytes(8));
$request = [
'taskType' => 'email.draft',
'input' => ['purpose' => 'Confirm the PHP SDK integration.'],
'idempotencyKey' => $idempotencyKey,
'constraints' => [
'maxOutputTokens' => 500,
'deadlineMilliseconds' => 30_000,
'minimumQuality' => 0.8,
],
];
$execution = $client->execute($request);
echo $execution['executionId'] . ' ' . $execution['status'] . PHP_EOL;
$execution = $client->execute(
'email.draft',
['purpose' => 'Confirm the PHP SDK integration.'],
);
$terminal = ['Succeeded', 'Failed', 'Cancelled', 'TimedOut'];
while (!in_array($execution['status'], $terminal, true)) {
usleep(500_000);
$current = $client->get($execution['executionId']);
if ($current === null) {
throw new RuntimeException('Execution is not visible to this tenant.');
}
$execution = $current;
}
if ($execution['status'] === 'Succeeded') {
echo json_encode($execution['outcome']['output'], JSON_PRETTY_PRINT) . PHP_EOL;
} else {
fwrite(STDERR, ($execution['error']['code'] ?? 'unknown') . ': ' .
($execution['error']['detail'] ?? 'execution failed') . PHP_EOL);
}
$lastSequence = 0;
foreach ($client->streamEvents($execution['executionId'], $lastSequence) as $event) {
$lastSequence = (int) $event['sequence'];
echo $lastSequence . ' ' . $event['type'] . PHP_EOL;
}
$accepted = $client->cancel($execution['executionId']);
if (!$accepted) echo "Execution was not found.\n";
$result = $client->submitApproval($execution['executionId'], [
'actionId' => $actionId,
'approved' => true,
'reason' => 'Reviewed by an authorized operator',
]);
$execution = $result['execution'];
$artifacts = $execution['outcome']['artifacts'] ?? [];
if ($artifacts !== []) {
$artifact = $client->getArtifact($artifacts[0]['artifactId']);
echo json_encode($artifact['content'] ?? null, JSON_PRETTY_PRINT) . PHP_EOL;
}
$health = $client->getModelGatewayHealth();
echo $health['gatewayId'] . ' ' . $health['status'] . PHP_EOL;
$summary = $client->getObservabilitySummary([
'from' => gmdate(DATE_ATOM, time() - 86400),
'to' => gmdate(DATE_ATOM),
'taskType' => 'email.draft',
]);
echo $summary['totals']['executions'] . PHP_EOL;
$timeline = $client->getExecutionTimeline($execution['executionId']);
echo ($timeline['traceId'] ?? 'not found') . PHP_EOL;
use IRoute\IRouteApiException;
try {
$execution = $client->execute($request);
} catch (IRouteApiException $error) {
fwrite(STDERR, "HTTP {$error->status} {$error->errorCode}: {$error->detail}\n");
fwrite(STDERR, $error->responseBody . PHP_EOL);
}
bash
composer install
php tests/conformance.php