Labs64 / NetLicensing-Community

NetLicensing Community Support
https://netlicensing.io
MIT License
5 stars 1 forks source link

How to validate customer licensing using PHP client #16

Closed r-brown closed 3 years ago

r-brown commented 3 years ago

Question Vendor request

I'm using NetLicensing PHP Client and wondering how to implement validation call to NetLicensing services.

Additional context API Key: disclosed licenseeNumber: disclosed

r-brown commented 3 years ago

To use NetLicensing PHP Client you need to add this library as dependency to your project. For instance via, composer.json:

    "require": {
        "Labs64/NetLicensingClient-php": "~2.4.0",
    },

Recommended NetLicensing authentication option is: API Key (needed role for validate endpoint is Licensee). You can easily create an API Key via Management Console / Settings / API Access

Screenshot 2020-11-19 at 14 44 44

Use your generated API Key number and Customer number in the PHP code snippet below:

const SECURITY_MODE = \NetLicensing\Constants::APIKEY_IDENTIFICATION;
const APIKEY = 'YOUR-API-KEY';
const LICENSEE = 'CUSTOMER-NUMBER';
try {
    $context = new \NetLicensing\Context();
    $context->setSecurityMode(SECURITY_MODE);
    $context->setApiKey(APIKEY);
    $validationParameters = new \NetLicensing\ValidationParameters();
    $validationResult = \NetLicensing\LicenseeService::validate($context, LICENSEE, $validationParameters);
    print_r($validationResult);
} catch (Exception $exception) {
    print_r($exception->getMessage());

Resulting validationResult contains received validation result object, which need to be interpreted in your application.

NetLicensing\ValidationResults Object
(
    [validators:protected] => Array
        (
            [M12345678] => Array
                (
                    [productModuleNumber] => M12345678
                    [valid] => true
                    [expires] => 2021-02-13T00:00:00Z
                    [productModuleName] => Module One
                    [licensingModel] => Subscription
                )
        )
    [ttl:protected] => DateTime Object
        (
            [date] => 2020-11-20 13:32:32.682000
            [timezone_type] => 2
            [timezone] => Z
        )