PHP code example of tweekersnut / license-sdk

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

    

tweekersnut / license-sdk example snippets



TweekersNut\LicenseSDK\LicenseClient;

$license = new LicenseClient(
    'https://license.yourdomain.com',  // Your license server URL
    file_get_contents('public.pem'),    // Public key for verification
    'license.tnlic'                     // License file path (optional)
);

try {
    $result = $license->activate('TNPRO-XXXXX-XXXXX-XXXXX-XXXXX');
    echo "License activated successfully!\n";
} catch (\TweekersNut\LicenseSDK\Exceptions\LicenseException $e) {
    echo "Activation failed: " . $e->getMessage() . "\n";
}

try {
    $result = $license->validate(true); // true = online validation
    
    if ($result['valid']) {
        echo "License is valid!\n";
        // Your application code here
    } else {
        echo "License invalid: " . $result['reason'] . "\n";
    }
} catch (\TweekersNut\LicenseSDK\Exceptions\ValidationException $e) {
    echo "Validation error: " . $e->getMessage() . "\n";
}

if ($license->hasFeature('premium')) {
    // Show premium features
    echo "Premium features enabled!\n";
} else {
    // Show basic features only
    echo "Upgrade to unlock premium features\n";
}

// Get all enabled features
$features = $license->getFeatures();
print_r($features);


TweekersNut\LicenseSDK\LicenseClient;
use TweekersNut\LicenseSDK\Exceptions\LicenseException;

// Initialize
$license = new LicenseClient(
    'https://license.yourdomain.com',
    file_get_contents('public.pem'),
    'license.tnlic'
);

// Check if already activated
if (!$license->isActivated()) {
    // Show activation form
    echo "Please enter your license key: ";
    $licenseKey = trim(fgets(STDIN));
    
    try {
        $license->activate($licenseKey);
        echo "✓ License activated successfully!\n";
    } catch (LicenseException $e) {
        die("✗ Activation failed: " . $e->getMessage() . "\n");
    }
}

// Validate license
try {
    $result = $license->validate(true);
    
    if ($result['valid']) {
        echo "✓ License is valid\n";
        
        // Check features
        if ($license->hasFeature('api_access')) {
            echo "✓ API access enabled\n";
        }
        
        if ($license->hasFeature('premium')) {
            echo "✓ Premium features enabled\n";
        }
        
        // Your application logic here
        runApplication();
        
    } else {
        die("✗ License invalid: " . $result['reason'] . "\n");
    }
} catch (LicenseException $e) {
    die("✗ Validation error: " . $e->getMessage() . "\n");
}

function runApplication() {
    echo "\n🚀 Application is running...\n";
    // Your application code here
}

public function __construct(
    string $serverUrl,
    string $publicKey,
    ?string $licenseFile = null
)

class MyPremiumPlugin {
    private $license;
    
    public function __construct() {
        $this->license = new LicenseClient(
            'https://license.yourdomain.com',
            file_get_contents(plugin_dir_path(__FILE__) . 'public.pem'),
            plugin_dir_path(__FILE__) . 'license.tnlic'
        );
        
        add_action('admin_init', [$this, 'check_license']);
    }
    
    public function check_license() {
        if (!$this->license->isActivated()) {
            add_action('admin_notices', function() {
                echo '<div class="error"><p>Please activate your license</p></div>';
            });
            return;
        }
        
        try {
            $result = $this->license->validate();
            if (!$result['valid']) {
                add_action('admin_notices', function() use ($result) {
                    echo '<div class="error"><p>License error: ' . $result['reason'] . '</p></div>';
                });
            }
        } catch (Exception $e) {
            // Handle error
        }
    }
}

// In a Service Provider
public function boot() {
    $license = new LicenseClient(
        config('license.server_url'),
        file_get_contents(storage_path('app/public.pem')),
        storage_path('app/license.tnlic')
    );
    
    $this->app->instance('license', $license);
}

// In a Middleware
public function handle($request, Closure $next) {
    $license = app('license');
    
    if (!$license->isActivated()) {
        return redirect()->route('license.activate');
    }
    
    try {
        $result = $license->validate();
        if (!$result['valid']) {
            return response()->json(['error' => 'Invalid license'], 403);
        }
    } catch (Exception $e) {
        return response()->json(['error' => 'License error'], 500);
    }
    
    return $next($request);
}

// bootstrap.php
LicenseClient(
    getenv('LICENSE_SERVER_URL'),
    file_get_contents(__DIR__ . '/public.pem'),
    __DIR__ . '/license.tnlic'
);

// Validate on startup
if (!$license->isActivated() || !$license->validate()['valid']) {
    die("License 

use TweekersNut\LicenseSDK\Exceptions\LicenseException;
use TweekersNut\LicenseSDK\Exceptions\ValidationException;
use TweekersNut\LicenseSDK\Exceptions\NetworkException;

try {
    $license->activate($key);
} catch (NetworkException $e) {
    // Network/connectivity issues
    echo "Network error: " . $e->getMessage();
} catch (ValidationException $e) {
    // Validation-specific errors
    echo "Validation error: " . $e->getMessage();
} catch (LicenseException $e) {
    // General license errors
    echo "License error: " . $e->getMessage();
}