getkey/licencephp

getkey 是一个可供每个人使用的许可证管理应用

v1.0.0 2024-08-30 15:51 UTC

This package is auto-updated.

Last update: 2024-09-30 15:59:40 UTC


README

支持:Laravel 框架,CI

简介

GetKey PHP API 客户端库 允许将各种数据验证功能集成到您的应用程序中。有关更多信息,请访问 getkey.my.id

要求

要使用 GetKey API 客户端,您需要以下内容

  • GetKey 账户
  • 使用生成的 API 密钥创建的应用程序项目

安装

要开始使用 GetKey PHP API 客户端库,请按照以下安装步骤操作

使用 Composer

  1. 使用 Composer 安装库

    composer require getkey/licencephp
  2. 在您的 PHP 脚本中包含 Composer 自动加载器

    require_once 'vendor/autoload.php';

不使用 Composer

  1. 下载库并将其添加到您的项目中。

  2. 在您的 PHP 脚本中包含文件 vendor/autoload.php

    include_once "getkey-licence-api-client/vendor/autoload.php";

如果您遇到错误,请尝试添加以下内容

require_once __DIR__ . '../vendor/getkey/licencephp/src/LicenceManager.php';

入门指南

要开始验证和使用 API,在您的框架中创建一个 Traits 系统。您需要首先在 getkey.my.id 网站上创建许可证,然后您可以使用许可证名称来调用许可证 API。

许可证验证完整示例

创建一个用于管理和验证许可证的 trait

trait ManagesLicense
{
    protected $licenceManager;
    protected $pemFilePath;

    // Automatically initialize when the trait is used
    public function __construct()
    {
        $this->initializeLicenseManager();
    }

    // Initialize the LicenseManager instance
    protected function initializeLicenseManager($endpoint = null)
    {
        $endpoint = $endpoint ?: config('services.licence.endpoint');
        $this->licenceManager = new LicenseManager($endpoint);
    }

    // Set the PEM file path based on the script name
    protected function setPemFilePath($scriptName)
    {
        $this->pemFilePath = storage_path('licences/' . $scriptName . '.pem');
    }

    // Get the PEM file path
    protected function getPemFilePath()
    {
        return $this->pemFilePath;
    }

    // Combined method to manage and validate the licence
    public function manageAndValidateLicense($scriptName)
    {
        try {
            if (!$this->licenceManager) {
                $this->initializeLicenseManager();
            }

            // Set the PEM file path using the script name
            $this->setPemFilePath($scriptName);
            $pemFilePath = $this->getPemFilePath();

            // Manage the licence (fetch, save, and validate)
            $this->licenceManager->manageLicense($scriptName, $pemFilePath);

            // Validate the PEM file content
            return $this->licenceManager->verifyPemContent($pemFilePath);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}

设置服务

将以下设置添加到服务配置中,例如在 Laravel 的 config/services.php 中

'licence' => [
    'endpoint' => env('LICENCE_ENDPOINT', 'https://getkey.my.id/api/v1/getLicence'),
],

或者,您可以在 .env 文件中设置它

控制器中验证的示例

use App\Traits\ManagesLicense;

class YourController extends Controller
{
    use ManagesLicense;

    public function yourMethod()
    {
        $scriptName = "Licence1";

        // Manage and validate the licence
        $result = $this->manageAndValidateLicense($scriptName);

        if ($result === true) {
            // Logic for successful validation
            return view('welcome');
        } else {
            // Logic for failed validation
            return response()->json(['error' => $result], 500);
        }
    }
}

响应类

每个请求都会返回 Response 类,提供处理响应的方法,具体请参阅库文档。

测试

getkey 仍处于测试阶段,您现在可以尝试使用它。报告类 licence 或 api 中的任何错误。请注意

对于 API 端点,它尚未通过令牌验证,仅通过 licencename 验证,但具有哈希安全...然后,许可证由一个用户使用,就不能在同一个项目中由两个用户使用。