Official PHP SDK for LicenseChain - Secure license management for PHP applications.
- π Secure Authentication - User registration, login, and session management
- π License Management - Create, validate, update, and revoke licenses
- π‘οΈ Hardware ID Validation - Prevent license sharing and unauthorized access
- π Webhook Support - Real-time license events and notifications
- π Analytics Integration - Track license usage and performance metrics
- β‘ High Performance - Optimized for production workloads
- π Async Operations - Non-blocking HTTP requests and data processing
- π οΈ Easy Integration - Simple API with comprehensive documentation
# Install via Composer
composer require licensechain/sdk
# Or add to composer.json
composer require licensechain/sdk:^1.0- Download the latest release from GitHub Releases
- Extract to your project directory
- Include the autoloader
# Add to composer.json
{
"repositories": [
{
"type": "git",
"url": "https://github.com/LicenseChain/LicenseChain-PHP-SDK.git"
}
],
"require": {
"licensechain/sdk": "dev-main"
}
}<?php
require_once 'vendor/autoload.php';
use LicenseChain\SDK\LicenseChainClient;
use LicenseChain\SDK\LicenseChainConfig;
// Initialize the client
$config = new LicenseChainConfig([
'apiKey' => 'your-api-key',
'appName' => 'your-app-name',
'version' => '1.0.0'
]);
$client = new LicenseChainClient($config);
// Connect to LicenseChain
try {
$client->connect();
echo "Connected to LicenseChain successfully!\n";
} catch (Exception $e) {
echo "Failed to connect: " . $e->getMessage() . "\n";
}
?>// Register a new user
try {
$user = $client->register('username', 'password', 'email@example.com');
echo "User registered successfully!\n";
echo "User ID: " . $user->getId() . "\n";
} catch (Exception $e) {
echo "Registration failed: " . $e->getMessage() . "\n";
}
// Login existing user
try {
$user = $client->login('username', 'password');
echo "User logged in successfully!\n";
echo "Session ID: " . $user->getSessionId() . "\n";
} catch (Exception $e) {
echo "Login failed: " . $e->getMessage() . "\n";
}// Validate a license
try {
$license = $client->validateLicense('LICENSE-KEY-HERE');
echo "License is valid!\n";
echo "License Key: " . $license->getKey() . "\n";
echo "Status: " . $license->getStatus() . "\n";
echo "Expires: " . $license->getExpires() . "\n";
echo "Features: " . implode(', ', $license->getFeatures()) . "\n";
echo "User: " . $license->getUser() . "\n";
} catch (Exception $e) {
echo "License validation failed: " . $e->getMessage() . "\n";
}
// Get user's licenses
try {
$licenses = $client->getUserLicenses();
echo "Found " . count($licenses) . " licenses:\n";
foreach ($licenses as $index => $license) {
echo " " . ($index + 1) . ". " . $license->getKey()
. " - " . $license->getStatus()
. " (Expires: " . $license->getExpires() . ")\n";
}
} catch (Exception $e) {
echo "Failed to get licenses: " . $e->getMessage() . "\n";
}// Get hardware ID (automatically generated)
$hardwareId = $client->getHardwareId();
echo "Hardware ID: " . $hardwareId . "\n";
// Validate hardware ID with license
try {
$isValid = $client->validateHardwareId('LICENSE-KEY-HERE', $hardwareId);
if ($isValid) {
echo "Hardware ID is valid for this license!\n";
} else {
echo "Hardware ID is not valid for this license.\n";
}
} catch (Exception $e) {
echo "Hardware ID validation failed: " . $e->getMessage() . "\n";
}// Set up webhook handler
$client->setWebhookHandler(function($event, $data) {
echo "Webhook received: " . $event . "\n";
switch ($event) {
case 'license.created':
echo "New license created: " . $data['licenseKey'] . "\n";
break;
case 'license.updated':
echo "License updated: " . $data['licenseKey'] . "\n";
break;
case 'license.revoked':
echo "License revoked: " . $data['licenseKey'] . "\n";
break;
}
});
// Start webhook listener
$client->startWebhookListener();All endpoints automatically use the /v1 prefix when connecting to https://api.licensechain.app.
- Production:
https://api.licensechain.app/v1 - Development:
https://api.licensechain.app/v1
| Method | Endpoint | Description |
|---|---|---|
GET |
/v1/health |
Health check |
POST |
/v1/auth/login |
User login |
POST |
/v1/auth/register |
User registration |
GET |
/v1/apps |
List applications |
POST |
/v1/apps |
Create application |
GET |
/v1/licenses |
List licenses |
POST |
/v1/licenses/verify |
Verify license |
GET |
/v1/webhooks |
List webhooks |
POST |
/v1/webhooks |
Create webhook |
GET |
/v1/analytics |
Get analytics |
Note: The SDK automatically prepends /v1 to all endpoints, so you only need to specify the path (e.g., /auth/login instead of /v1/auth/login).
$config = new LicenseChainConfig([
'apiKey' => 'your-api-key',
'appName' => 'your-app-name',
'version' => '1.0.0',
'baseUrl' => 'https://api.licensechain.app' // Optional
]);
$client = new LicenseChainClient($config);// Connect to LicenseChain
$client->connect();
// Disconnect from LicenseChain
$client->disconnect();
// Check connection status
$isConnected = $client->isConnected();// Register a new user
$user = $client->register($username, $password, $email);
// Login existing user
$user = $client->login($username, $password);
// Logout current user
$client->logout();
// Get current user info
$user = $client->getCurrentUser();// Validate a license
$license = $client->validateLicense($licenseKey);
// Get user's licenses
$licenses = $client->getUserLicenses();
// Create a new license
$license = $client->createLicense($userId, $features, $expires);
// Update a license
$license = $client->updateLicense($licenseKey, $updates);
// Revoke a license
$client->revokeLicense($licenseKey);
// Extend a license
$license = $client->extendLicense($licenseKey, $days);// Get hardware ID
$hardwareId = $client->getHardwareId();
// Validate hardware ID
$isValid = $client->validateHardwareId($licenseKey, $hardwareId);
// Bind hardware ID to license
$client->bindHardwareId($licenseKey, $hardwareId);// Set webhook handler
$client->setWebhookHandler($handler);
// Start webhook listener
$client->startWebhookListener();
// Stop webhook listener
$client->stopWebhookListener();// Track event
$client->trackEvent($eventName, $properties);
// Get analytics data
$analytics = $client->getAnalytics($timeRange);Set these in your environment or through your build process:
# Required
export LICENSECHAIN_API_KEY=your-api-key
export LICENSECHAIN_APP_NAME=your-app-name
export LICENSECHAIN_APP_VERSION=1.0.0
# Optional
export LICENSECHAIN_BASE_URL=https://api.licensechain.app
export LICENSECHAIN_DEBUG=true$config = new LicenseChainConfig([
'apiKey' => 'your-api-key',
'appName' => 'your-app-name',
'version' => '1.0.0',
'baseUrl' => 'https://api.licensechain.app',
'timeout' => 30, // Request timeout in seconds
'retries' => 3, // Number of retry attempts
'debug' => false, // Enable debug logging
'userAgent' => 'MyApp/1.0.0' // Custom user agent
]);The SDK automatically generates and manages hardware IDs to prevent license sharing:
// Hardware ID is automatically generated and stored
$hardwareId = $client->getHardwareId();
// Validate against license
$isValid = $client->validateHardwareId($licenseKey, $hardwareId);- All API requests use HTTPS
- API keys are securely stored and transmitted
- Session tokens are automatically managed
- Webhook signatures are verified
- Real-time license validation
- Hardware ID binding
- Expiration checking
- Feature-based access control
// Track custom events
$client->trackEvent('app.started', [
'level' => 1,
'playerCount' => 10
]);
// Track license events
$client->trackEvent('license.validated', [
'licenseKey' => 'LICENSE-KEY',
'features' => 'premium,unlimited'
]);// Get performance metrics
$metrics = $client->getPerformanceMetrics();
echo "API Response Time: " . $metrics->getAverageResponseTime() . "ms\n";
echo "Success Rate: " . number_format($metrics->getSuccessRate() * 100, 2) . "%\n";
echo "Error Count: " . $metrics->getErrorCount() . "\n";try {
$license = $client->validateLicense('invalid-key');
} catch (InvalidLicenseException $e) {
echo "License key is invalid\n";
} catch (ExpiredLicenseException $e) {
echo "License has expired\n";
} catch (NetworkException $e) {
echo "Network connection failed\n";
} catch (LicenseChainException $e) {
echo "LicenseChain error: " . $e->getMessage() . "\n";
}// Automatic retry for network errors
$config = new LicenseChainConfig([
'apiKey' => 'your-api-key',
'appName' => 'your-app-name',
'version' => '1.0.0',
'retries' => 3, // Retry up to 3 times
'timeout' => 30 // Wait 30 seconds for each request
]);# Run tests
composer test
# Run tests with coverage
composer test:coverage
# Run specific test
vendor/bin/phpunit tests/ClientTest.php# Test with real API
composer test:integrationSee the examples/ directory for complete examples:
basic_usage.php- Basic SDK usageadvanced_features.php- Advanced features and configurationwebhook_integration.php- Webhook handling
We welcome contributions! Please see our Contributing Guide for details.
- Clone the repository
- Install PHP 8.0 or later
- Install Composer 2.0 or later
- Install dependencies:
composer install - Build:
composer build - Test:
composer test
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: https://docs.licensechain.app/php
- Issues: GitHub Issues
- Discord: LicenseChain Discord
- Email: support@licensechain.app
- LicenseChain JavaScript SDK
- LicenseChain Python SDK
- LicenseChain Node.js SDK
- LicenseChain Customer Panel
Made with β€οΈ for the PHP community