Skip to content

Remote PHP Quick Start

The remote PHP client lets your application use graphs executed by an ExecKernel API. The service loads the graph, evaluates transitions and stores the session; your PHP application sends the input and uses the returned data to build its interface. In this quick start, you will learn to start a session from PHP, submit a choice and retrieve the session later.

We will practise with the example graph from Your first graph, which represents a choice between collecting an order from a store and having it delivered. Delivery is available only for addresses the store serves. We will use this example to turn the available choices and the engine’s result into data a PHP template can display.

You need the PHP package installed in your application and access to an API that serves the delivery-options graph from Your first graph. If you are setting up that service yourself, complete the HTTP API Quick Start first. The remote application does not need its own copy of the graph file.

The examples read two environment variables: EXECKERNEL_BASE_URL, the service’s base URL, and EXECKERNEL_API_TOKEN, its access token. The API quick start prepares these variables. For a different deployment, use the URL and token supplied for that service. Keep the token in your server environment; it is the credential used by this PHP application to call the API.

The default transport used here needs PHP’s allow_url_fopen setting enabled. The base URL is the service root, such as http://127.0.0.1:4320; the SDK adds the /execkernel/… operation paths.

Two classes are enough to create the remote client. Config describes the connection, and ExecKernelHttpClient performs the HTTP operations and converts the responses into PHP objects.

use ExecKernel\Client\ExecKernelHttpClient;
use ExecKernel\Config\Config;
$baseUrl = getenv('EXECKERNEL_BASE_URL');
$token = getenv('EXECKERNEL_API_TOKEN');
if (!$baseUrl || !$token) {
throw new RuntimeException('Set EXECKERNEL_BASE_URL and EXECKERNEL_API_TOKEN.');
}
$client = new ExecKernelHttpClient(new Config(
baseUrl: $baseUrl,
authToken: $token,
));

The client sends the token as a Bearer credential. You do not need to assemble request headers, encode JSON or instantiate an engine inside this application.

Starting a session creates an individual execution of the graph. The graph’s identifier is delivery-options; the context provides the facts for this execution. Here, we supply delivery_available => true to represent an address that the application has already checked and can serve.

$state = $client->start(
'delivery-options',
['delivery_available' => true],
'en'
);
$sessionId = $state->sessionId;

The result is a SessionStateDTO: a PHP data object containing the session identifier, the current node, the context and the current payload. The payload is the information the application can present at that point. At node 1, it is a NodePayloadDTO containing the question and its outgoing choices.

For a PHP template, we can extract a heading and one entry per button. The edge identifier becomes the value the application sends when the customer chooses that button.

use ExecKernel\DTO\NodePayloadDTO;
$payload = $state->payload;
if (!$payload instanceof NodePayloadDTO) {
throw new LogicException('Expected delivery choices.');
}
$view = [
'heading' => $payload->title,
'choices' => array_map(static fn (array $edge): array => [
'edgeId' => $edge['id'],
'label' => $edge['labels'][0]['text'],
'enabled' => $edge['enabled'],
], $payload->edges),
];

$view is an application-defined array, not another ExecKernel response type. Its heading is “How would you like to receive your order?”, and its choices are:

Button text Value to submit Enabled
Collect from store 10 Yes
Home delivery 20 Yes

This graph supplies one label per edge in the requested language, so the example uses the first entry in labels. A template can render each label and use enabled to control the button. As with other text received from a service, escape the text when inserting it into HTML.

When the customer selects home delivery, the application sends edge 20. The API checks that transition against the stored session and the graph’s condition, then returns the state at terminal node 3.

$state = $client->navigate($state, 20);

Assign the returned value: navigate() returns a new SessionStateDTO, leaving the previous object unchanged. There are now no more graph choices to display. The terminal payload contains the content the application needs for its confirmation.

use ExecKernel\DTO\TerminalPayloadDTO;
$payload = $state->payload;
if (!$payload instanceof TerminalPayloadDTO) {
throw new LogicException('Expected a delivery result.');
}
$confirmation = [
'heading' => $payload->title,
'messages' => array_column($payload->outputs, 'content'),
];

For this graph, the heading is “Home delivery selected” and the message is “Confirm the delivery address for your order.” The application can replace its choice form with that confirmation. Organizing the shipment or updating an order remains application work; reaching this terminal supplies the selected outcome and its content.

If delivery_available had been false, the initial payload would have marked home delivery as blocked. Rendering a disabled button helps the user understand their options, while the API still checks the requested transition.

In an actual application, starting the session, receiving the customer’s choice and displaying the result may happen in different HTTP requests. Save $sessionId alongside the relevant application record so a later request can retrieve the execution.

$state = $client->resume($sessionId, 'en');

resume() asks the API for its stored session. After the selection above, it returns node 3 and the home-delivery payload; it does not start again at the root. The stored session must still exist and be within the service’s retention period.

SDK failures raise an ExecKernelException. Its code() method gives the ExecKernel error code, and getMessage() gives the message. If a connection fails after sending a choice, the remote operation may already have completed: retrieve the session before deciding whether another transition is needed.

The complete PHP example combines the operations above with a deliveryView() function that prepares either choices or confirmation messages. Save it as remote-delivery.php in your PHP project’s root, next to vendor/, and run it with the connection variables already set:

Terminal window
php remote-delivery.php

It displays the two application views, the session identifier and resumedNode: 3. This gives you a compact way to inspect what a template would receive at each stage. The script selects edge 20 automatically for demonstration; in your application, that value comes from the customer’s submitted choice.

Complete PHP program
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use ExecKernel\Client\ExecKernelHttpClient;
use ExecKernel\Config\Config;
use ExecKernel\DTO\NodePayloadDTO;
use ExecKernel\DTO\TerminalPayloadDTO;
use ExecKernel\Exceptions\ExecKernelException;
function deliveryView(NodePayloadDTO|TerminalPayloadDTO|null $payload): array
{
if ($payload instanceof NodePayloadDTO) {
return [
'heading' => $payload->title,
'choices' => array_map(static fn (array $edge): array => [
'edgeId' => $edge['id'],
'label' => $edge['labels'][0]['text'],
'enabled' => $edge['enabled'],
], $payload->edges),
];
}
if ($payload instanceof TerminalPayloadDTO) {
$messages = [];
foreach ($payload->outputs as $output) {
if ($output['type'] === 'content' && isset($output['content'])) {
$messages[] = $output['content'];
}
}
return [
'heading' => $payload->title,
'messages' => $messages,
];
}
throw new RuntimeException('The service did not return a displayable payload.');
}
$baseUrl = getenv('EXECKERNEL_BASE_URL');
$token = getenv('EXECKERNEL_API_TOKEN');
$lang = getenv('EXECKERNEL_LANG') ?: 'en';
if (!$baseUrl || !$token) {
throw new RuntimeException('Set EXECKERNEL_BASE_URL and EXECKERNEL_API_TOKEN.');
}
$client = new ExecKernelHttpClient(new Config(
baseUrl: $baseUrl,
authToken: $token,
));
try {
$state = $client->start('delivery-options', ['delivery_available' => true], $lang);
$sessionId = $state->sessionId;
$choices = deliveryView($state->payload);
$state = $client->navigate($state, 20);
$confirmation = deliveryView($state->payload);
$resumed = $client->resume($sessionId, $lang);
echo json_encode([
'sessionId' => $sessionId,
'choices' => $choices,
'confirmation' => $confirmation,
'resumedNode' => $resumed->nodeId,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), PHP_EOL;
} catch (ExecKernelException $error) {
fwrite(STDERR, $error->code() . ': ' . $error->getMessage() . PHP_EOL);
exit(1);
}

To explore how context changes available choices, open the Showcase’s Context and conditions demo. Change the inputs and compare the enabled transitions before selecting one. For a JavaScript consumer of the same API, see the JavaScript & TypeScript Quick Start.