PHP Projects

SunLicense Integration Guide for PHP Projects

Prerequisites

  • PHP 7.4 or higher

  • Composer (optional)

  • cURL extension enabled

  • SunLicense API credentials

Integration Steps

1. Create License Validator Class

<?php

class SunLicenseValidator {
    private string $licenseKey;
    private int $productId;
    private string $apiUrl;
    private string $version;

    public function __construct(string $licenseKey, int $productId, string $version = '1.0.0') {
        $this->licenseKey = $licenseKey;
        $this->productId = $productId;
        $this->version = $version;
        $this->apiUrl = 'YOUR_API_URL/api/v1/validate';
    }

    public function validate(): bool {
        $data = [
            'licenseKey' => $this->licenseKey,
            'productId' => $this->productId,
            'productVersion' => $this->version,
            'hwid' => $this->getHWID() // Optional
        ];

        $ch = curl_init($this->apiUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json'
        ]);

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

        if ($httpCode !== 200) {
            throw new Exception('License validation failed: ' . $response);
        }

        return true;
    }

    private function getHWID(): string {
        // Implement your HWID generation logic here
        return 'YOUR-HWID';
    }
}

2. Implementation Example

3. Using with Framework (Laravel Example)

Best Practices

  1. Error Handling

    • Implement proper try-catch blocks

    • Log validation failures

    • Graceful error handling

  2. Caching

    • Cache validation results

    • Implement proper cache invalidation

    • Handle offline scenarios

  3. Security

    • Store license key in environment variables

    • Implement proper error logging

    • Secure API communication

Common Issues and Solutions

  1. cURL Errors

    • Verify cURL installation

    • Check SSL certificates

    • Confirm API endpoint accessibility

  2. Validation Failures

    • Check license key validity

    • Verify product ID

    • Monitor API response codes

Last updated