licensechain / sdk
Official LicenseChain PHP SDK for license management and validation
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/licensechain/sdk
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- php-cs-fixer/shim: ^3.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.0
README
Official PHP SDK for LicenseChain - Secure license management for PHP applications.
๐ Features
- ๐ 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
๐ฆ Installation
Method 1: Composer (Recommended)
# Install via Composer composer require licensechain/sdk # Or add to composer.json composer require licensechain/sdk:^1.0
Method 2: Manual Installation
- Download the latest release from GitHub Releases
- Extract to your project directory
- Include the autoloader
Method 3: Git Repository
# Add to composer.json { "repositories": [ { "type": "git", "url": "https://github.com/LicenseChain/LicenseChain-PHP-SDK.git" } ], "require": { "licensechain/sdk": "dev-main" } }
๐ Quick Start
Basic Setup
<?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"; } ?>
User Authentication
// 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"; }
License Management
// 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"; }
Hardware ID Validation
// 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"; }
Webhook Integration
// 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();
๐ API Endpoints
All endpoints automatically use the /v1 prefix when connecting to https://api.licensechain.app.
Base URL
- Production:
https://api.licensechain.app/v1 - Development:
https://api.licensechain.app/v1
Available Endpoints
| 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).
๐ API Reference
LicenseChainClient
Constructor
$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);
Methods
Connection Management
// Connect to LicenseChain $client->connect(); // Disconnect from LicenseChain $client->disconnect(); // Check connection status $isConnected = $client->isConnected();
User Authentication
// 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();
License Management
// 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);
Hardware ID Management
// Get hardware ID $hardwareId = $client->getHardwareId(); // Validate hardware ID $isValid = $client->validateHardwareId($licenseKey, $hardwareId); // Bind hardware ID to license $client->bindHardwareId($licenseKey, $hardwareId);
Webhook Management
// Set webhook handler $client->setWebhookHandler($handler); // Start webhook listener $client->startWebhookListener(); // Stop webhook listener $client->stopWebhookListener();
Analytics
// Track event $client->trackEvent($eventName, $properties); // Get analytics data $analytics = $client->getAnalytics($timeRange);
๐ง Configuration
Environment Variables
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
Advanced Configuration
$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 ]);
๐ก๏ธ Security Features
Hardware ID Protection
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);
Secure Communication
- All API requests use HTTPS
- API keys are securely stored and transmitted
- Session tokens are automatically managed
- Webhook signatures are verified
License Validation
- Real-time license validation
- Hardware ID binding
- Expiration checking
- Feature-based access control
๐ Analytics and Monitoring
Event Tracking
// Track custom events $client->trackEvent('app.started', [ 'level' => 1, 'playerCount' => 10 ]); // Track license events $client->trackEvent('license.validated', [ 'licenseKey' => 'LICENSE-KEY', 'features' => 'premium,unlimited' ]);
Performance Monitoring
// 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";
๐ Error Handling
Custom Exception Types
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"; }
Retry Logic
// 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 ]);
๐งช Testing
Unit Tests
# Run tests composer test # Run tests with coverage composer test:coverage # Run specific test vendor/bin/phpunit tests/ClientTest.php
Integration Tests
# Test with real API
composer test:integration
๐ Examples
See the examples/ directory for complete examples:
basic_usage.php- Basic SDK usageadvanced_features.php- Advanced features and configurationwebhook_integration.php- Webhook handling
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- 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
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
- Documentation: https://docs.licensechain.app/php
- Issues: GitHub Issues
- Discord: LicenseChain Discord
- Email: support@licensechain.app
๐ Related Projects
- LicenseChain JavaScript SDK
- LicenseChain Python SDK
- LicenseChain Node.js SDK
- LicenseChain Customer Panel
Made with โค๏ธ for the PHP community