Skip to content

PHP Projects

Integrate abystrixLicense into your PHP web application or CLI tool.

Using cURL

php
<?php

class abystrixLicense {
    private string $apiUrl;
    private string $apiToken;

    public function __construct(string $apiUrl, string $apiToken) {
        $this->apiUrl = rtrim($apiUrl, '/');
        $this->apiToken = $apiToken;
    }

    public function validate(string $licenseKey, int $productId, ?string $hwid = null): array {
        $ch = curl_init("{$this->apiUrl}/api/v1/validate");
        curl_setopt_array($ch, [
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
            CURLOPT_POSTFIELDS => json_encode([
                'licenseKey' => $licenseKey,
                'productId' => $productId,
                'hwid' => $hwid ?? php_uname()
            ])
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return json_decode($response, true) ?? ['valid' => false, 'message' => 'Connection failed'];
    }

    public function getLicense(string $licenseKey): ?array {
        $ch = curl_init("{$this->apiUrl}/api/v2/licenses/by-license-key/$licenseKey");
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ["TOKEN: {$this->apiToken}"]
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $httpCode === 200 ? json_decode($response, true) : null;
    }
}

// Usage
$license = new abystrixLicense('https://your-server.com', 'your-api-token');
$result = $license->validate('XXXXX-XXXXX-XXXXX-XXXXX', 1);
if ($result['valid']) {
    echo "License is valid!\n";
} else {
    echo "License invalid: " . ($result['message'] ?? 'Unknown error') . "\n";
}
?>

Using Guzzle (Laravel)

php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class LicenseService {
    private Client $client;

    public function __construct() {
        $this->client = new Client([
            'base_uri' => 'https://your-server.com',
            'headers' => ['TOKEN' => 'your-api-token']
        ]);
    }

    public function validateLicense(string $key, int $productId): bool {
        try {
            $response = $this->client->post('/api/v2/validate', [
                'json' => ['licenseKey' => $key, 'productId' => $productId]
            ]);
            $data = json_decode($response->getBody(), true);
            return $data['valid'] ?? false;
        } catch (GuzzleException $e) {
            return false;
        }
    }
}

Released under the MIT License.