Skip to navigation

PHP guide

This page walks through the whole PHP SDK in the order you would use it. Each step starts with the shortest code that works.

Download the PHP SDK if you have not done so yet.

Requirements

SupportedRecommended
PHP7.4 to 8.58.3 or newer
Extensionssodium and json, both bundled with PHPcurl
Windowsffi for protected storage, device login and update prompts

Without curl, the SDK uses PHP streams. On Linux and macOS, pass your own systemId and storage when you create the client, as shown in Options.

Add the SDK to your project

Unzip the download into a folder in your project, for example authlyx, and include its autoloader:

require __DIR__ . '/authlyx/autoload.php';
use AuthlyX\AuthlyX;

With Composer, add the folder as a path repository and require authlyx/authlyx:

{
"repositories": [{ "type": "path", "url": "authlyx" }],
"require": { "authlyx/authlyx": "*" }
}

Create the client

$app = new AuthlyX('YOUR_APP_KEY', '1.0.0');

The first value is your app’s AppKey. Find it in the dashboard under your application’s credentials. The second is the version of the program you are building. AuthlyX compares it with the versions you allow in the dashboard, so raise it each time you ship a new build. See Allowed Versions.

On PHP 8 you can name the arguments:

$app = new AuthlyX(appKey: 'YOUR_APP_KEY', version: '1.0.0');

Create one client for the whole script.

How calls work

Every call waits for the reply and returns a Result with ok, code, message and data. initialize() always returns a result, even without internet. Other calls throw AuthlyXException when AuthlyX refuses or cannot be reached:

use AuthlyX\AuthlyXException;
try {
$app->licenseLogin('YOUR_LICENSE_KEY');
} catch (AuthlyXException $error) {
echo $error->getErrorCode(), ': ', $error->getMessage(), "\n";
}

Start the SDK

$started = $app->initialize();
if (!$started->ok) {
echo $started->message, "\n";
exit(1);
}
if ($started->code === 'OFFLINE_MODE') {
echo $started->message, "\n";
}

initialize() connects to AuthlyX, checks that this version is allowed and loads your app’s settings. It must succeed before anything else. init() is the same call.

CodeSuccessMeaning
APP_INITIALIZEDYesConnected. Continue to sign-in.
OFFLINE_MODEYesNo internet, but this PC has saved offline access.
NETWORK_...NoNo internet and nothing saved. Offer to try again.
UPDATE_REQUIREDNoThis version is older than the one set in your dashboard.
VERSION_MISMATCHNoThis version is newer than the dashboard version and is not allowed.

Sign in

$app->licenseLogin('YOUR_LICENSE_KEY');
$app->login('YOUR_USERNAME', 'YOUR_PASSWORD');
$app->deviceLogin();

These are three ways to sign in. Use the one that matches the access type you created in the dashboard. login with one value signs in with a license key. Device login signs in the current Windows user with no form, and only works for devices you approved first; see Device login.

Create an account, change the password, add time

$app->registerUser('YOUR_USERNAME', 'YOUR_PASSWORD', 'YOUR_LICENSE_KEY');
$app->changePassword('CURRENT_PASSWORD', 'NEW_PASSWORD');
$app->redeemLicense('NEW_LICENSE_KEY');

registerUser creates the account with a license and signs it in. redeemLicense adds a license’s time to the signed-in account.

Read the account

$username = $app->userData->Username;
$plan = $app->userData['Plan'];
$expiry = $app->userData['ExpiryDate'];
$features = $app->userData['features'];
$videoQuality = $app->userData['featureValues']['videoQuality'];

userData works as an object or an array. It is empty until someone signs in. $app->userData->toArray() returns every field. The fields are listed in User Data.

Features and metadata

Features come from the plan and the account. Some are on/off, some carry a value such as "high" or 90. Metadata is extra information attached to accounts. See Features and metadata.

if (in_array('exportPdf', $app->userData['features'] ?? [], true)) {
enableExport();
}
$check = $app->checkFeature('exportPdf');
print_r($check->data);

getFeatures lists every feature and getFeature returns one. For metadata, use getAllMetadata, getMetadata and checkMetadata.

Save metadata

$app->setMetadata('display_name', 'Alex');
$app->setMetadata('launch_count', 12);

The value can be text, a number, a boolean or an array.

Cloud variables

$url = $app->getVariable('download_url')->data;
$app->updateVariable('maintenance', 'false');

For getVariable, data is the value itself. See Cloud variables.

Scripts

$script = $app->getScript('welcome_message');
$latest = $app->getScript('welcome_message', 3);

The second value is an optional revision. The SDK checks the script’s signature before returning it. It does not run the script. See Scripts.

Logs and webhooks

$app->log('User opened settings');
$app->sendWebhook('setup.completed', ['step' => 3]);

See Logs and Webhooks.

Staying signed in

PHP has no background threads. With the default server heartbeat, the SDK checks the session before a call once the interval set by the server has passed, and refreshes features and metadata each time. If you ban, pause or expire the account, that check ends the session.

if (!$app->hasAccess()) {
showLoginScreen();
}

In a long-running script, call $app->heartbeat() from your loop. It only contacts AuthlyX when a check is due, and returns null otherwise. To check right away, call $app->checkSession(); $app->onResume() does the same.

To be told when the session ends or the connection changes, pass functions when you create the client:

$app = new AuthlyX('YOUR_APP_KEY', '1.0.0', [
'onSessionInvalidated' => function (AuthlyXException $error) {
echo 'Signed out: ', $error->getMessage(), "\n";
},
'onConnection' => function (string $state) {
echo 'Connection: ', $state, "\n";
},
]);

onConnection receives online, disconnected, untrusted or disposed. More in Sessions and heartbeats.

Offline access

$app->licenseLogin($licenseKey);
$app->saveOfflineAccess();

Later, without internet, initialize() succeeds with OFFLINE_MODE, and the user signs in with the same license key or username and password. The SDK checks them against the saved permit and the sign-in succeeds with the code OFFLINE_ACCESS. $app->hasOfflineAccess() tells you whether this PC has a valid saved permit. Signing out deletes it. See Offline access.

Updates

When this version is not allowed, initialize() fails with UPDATE_REQUIRED or VERSION_MISMATCH. Read the details from the result:

$started = $app->initialize();
if (in_array($started->code, ['UPDATE_REQUIRED', 'VERSION_MISMATCH'], true)) {
$update = $started->data['update'] ?? [];
echo $started->message, ' ', $update['version'] ?? '', ' ', $update['url'] ?? '', "\n";
}

On Windows with ffi, pass 'updatePrompt' => true to show the Windows update box with a button that opens your download page. See Automatic updates.

Sign out and shut down

$app->logout();
$app->close();

logout ends the session, clears userData and deletes saved offline access on this PC, even when AuthlyX cannot be reached. close releases the client; a closed client cannot be used again.

Results and errors

MemberMeaning
$result->ok, $result->successWhether the call succeeded
$result->codeA stable code such as APP_INITIALIZED
$result->messageA message you can show to the user
$result->dataThe response data as arrays and values
$result->requestIdThe server request ID, for support

AuthlyXException has getErrorCode(), getMessage(), getKind(), getStatus() and getRequestId(). getKind() is server, network, security or storage. network means the SDK could not reach AuthlyX or confirm the answer; do not treat it as a ban. All codes are listed in Errors.

Options

The third argument is an array of options. Every option is optional on Windows.

OptionDefaultMeaning
systemIdWindows user SIDRequired on Linux and macOS
storageWindowsProtectedStoreWhere the installation key and offline access are saved. Required on Linux and macOS.
requestTimeout8.0Seconds allowed for each call, including retries
retryLimit2Retries for temporary network and server errors, from 0 to 5
heartbeatservermanual leaves session checks to you
updatePromptfalseShows the Windows update box. Needs ffi.
caBundlenoneA CA bundle file for HTTPS, if your PHP has none configured
onConnectionnoneCalled with the connection state
onSessionInvalidatednoneCalled with the AuthlyXException that ended the session

On Linux, for example:

use AuthlyX\Storage\FileStore;
$app = new AuthlyX('YOUR_APP_KEY', '1.0.0', [
'systemId' => trim(file_get_contents('/etc/machine-id')),
'storage' => new FileStore('/var/lib/your-app/authlyx'),
]);

FileStore writes files readable only by the current user. MemoryStore keeps nothing after the script ends. You can also implement AuthlyX\Storage\Store with your own protected storage.

A complete program

<?php
require __DIR__ . '/authlyx/autoload.php';
use AuthlyX\AuthlyX;
use AuthlyX\AuthlyXException;
$app = new AuthlyX('YOUR_APP_KEY', '1.0.0');
$started = $app->initialize();
if (!$started->ok) {
echo $started->message, "\n";
exit(1);
}
echo 'License key: ';
$licenseKey = trim((string) fgets(STDIN));
try {
$app->licenseLogin($licenseKey);
echo 'Signed in. Plan: ', $app->userData['Plan'], "\n";
$app->logout();
} catch (AuthlyXException $error) {
echo $error->getMessage(), "\n";
exit(1);
} finally {
$app->close();
}

The example on GitHub is a fuller console app with a menu for every feature. Each method also has its own page with every language side by side under SDK methods.