PHP code example of key-warden / sdk

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

    

key-warden / sdk example snippets


use KeyWarden\KeyWardenClient;

$res = KeyWardenClient::validate($licenceKey, [
    'apimKey'   => getenv('KW_APIM_KEY'),     // your APIM subscription key
    'clientKey' => getenv('KW_CLIENT_KEY'),   // your validation key
    'machineId' => KeyWardenClient::machineIdFrom(gethostname(), $userId), // stable, hashed your side
]);

if (!$res['valid']) {
    lockFeatures($res['reason'] ?? null);
}
// $res['token'] is a freshly signed proof — cache it for the offline path below.

$check = KeyWardenClient::verifyToken($cachedToken, getenv('KW_PUBLIC_KEY'));
if (!$check['valid']) {
    lockFeatures($check['reason']); // 'bad_signature' | 'expired' | ...
}

$res = KeyWardenClient::validateOrVerify($licenceKey, [
    'apimKey' => $apimKey, 'clientKey' => $clientKey, 'machineId' => $machineId,
    'cachedToken' => $lastGoodToken,          // from a previous validate()
    'publicKey'   => getenv('KW_PUBLIC_KEY'),
]);
// $res['source'] === 'online' | 'offline'

use KeyWarden\KeyWardenClient;
use KeyWarden\Grant;

$keys = [
    ['kid' => 'mitaa-k1', 'pub' => 'BASE64_32_BYTE_KEY'],
    ['kid' => 'mitaa-k2', 'pub' => 'BASE64_32_BYTE_KEY'],   // the incoming one
];

$res = KeyWardenClient::validate($licenceKey, [
    'apimKey'   => $apimKey,
    'clientKey' => $clientKey,
    'machineId' => $machineId,
    'keys'      => $keys,
    'product'   => 'acme-maps',
    'userCount' => $activeUsers,      // for banded plans
    'envType'   => 'production',      // or let KW_ENV_TYPE / WP_ENVIRONMENT_TYPE decide
    'siteLabel' => home_url(),        // optional, for the vendor console
]);

// $res['grantVerdict'] : 'accept' | 'deny' | 'fallback'  (absent if you baked no keys)
// $res['expiresAt']    : the licence term (NOT $res['claims']['exp'])

$g = Grant::verify($cachedToken, $keys, [
    'activationKey' => $licenceKey,
    'machineId'     => $machineId,
    'nonce'         => $nonce,   // only if you still hold the one you sent
]);
// $g === ['verdict' => ..., 'reason' => ..., 'claims' => [...]]
if ($g['verdict'] === 'accept')   { run_app(); }
elseif ($g['verdict'] === 'deny') { lock_features($g['reason']); }
else                              { keep_last_known_good(); }   // 'fallback'

$res = KeyWardenClient::verifyToken($cachedToken, getenv('KW_PUBLIC_KEY'));

if ($res['valid']) {
    $t = KeyWardenClient::trialInfo($res);   // ['isTrial','expired','expiresAt','secondsRemaining','daysRemaining']
    if ($t['isTrial']) {
        show_banner("Trial — {$t['daysRemaining']} day(s) left");
    }
    run_app();
} elseif (($res['reason'] ?? '') === 'expired') {
    show_paywall('Your trial has ended. Enter a licence key to continue.');
}

use KeyWarden\KeyWardenClient as KW;

// Build time — seal a file once:
file_put_contents('secret.sealed', KW::seal(file_get_contents('secret.php'), $MY_CONTENT_KEY_B64));

// Runtime — the key rides in the validate token as `ck`, machine-bound:
$res = KW::validate($licence, ['apimKey' => $apim, 'clientKey' => $ck, 'machineId' => $mid]);
$key  = KW::unlockFromToken($res['token'], $mid);   // raw content-key bytes
$code = KW::unseal($sealedBlob, $key);              // your decrypted file

// Or a live check every time (real-time revocation):
$key = KW::unsealOnline($licence, ['apimKey' => $apim, 'machineId' => $mid]);