planetthecloud/mofh-callback-client

PHP 为 MyOwnFreeHost 定制的 API 回调客户端

1.2.0 2022-05-06 12:10 UTC

This package is auto-updated.

Last update: 2024-09-06 17:28:37 UTC


README

一个解析来自 MyOwnFreeHost 的回调的 API 回调客户端。

安装

此包最好通过 Composer 安装

composer require planetthecloud/mofh-callback-client

使用方法

create 方法接受以下参数

  • ip: MyOwnFreeHost 的 IP 地址。

在哪里找到 MyOwnFreeHost 的 IP 地址?
您可以从 经销商面板 中找到 IP 地址。登录并转到 API -> 设置 WHM API -> 选择您要配置的域名,然后复制显示在 "连接到 MyOwnFreeHost 的 IP 地址" 旁边的 IP 地址。

回调处理器
以下方法接受一个可调用对象作为参数。
这些方法将根据 MyOwnFreeHost 的回调相应调用。
以下是方法列表和将传递给可调用对象的参数

  • onAccountActivated
    • username: 账户的 vPanel 用户名。
    • raw: 原始回调数据。
  • onAccountSuspended
    • username: 账户的 vPanel 用户名。
    • reason: 暂停的原因。
    • raw: 原始回调数据。
    • common_reason: 暂停的常见原因,请参阅下文的 parseCommonSuspensionReason 部分。
  • onAccountReactivated
    • username: 账户的 vPanel 用户名。
    • raw: 原始回调数据。
  • onSqlServer
    • username: 账户的 vPanel 用户名。
    • cluster: 账户的 SQL 集群。
    • raw: 原始回调数据。
  • onAccountDeleted
    • username: 账户的 vPanel 用户名。
    • raw: 原始回调数据。

处理器
handle 方法接受以下参数

  • data: 来自 MyOwnFreeHost 的回调数据。
  • ip: (可选) 调用者的 IP 地址。 不是 MyOwnFreeHost 的 IP 地址

检查回调
还有一些方法可以在处理前后检查回调。
以下方法接受一个可调用对象作为参数

  • beforeCallback: 在处理回调之前调用。
  • afterCallback: 在处理回调之后调用。

可调用对象将收到以下参数

  • data: 来自 MyOwnFreeHost 的回调数据。
  • ip: (可选) 调用者的 IP 地址。 不是 MyOwnFreeHost 的 IP 地址

beforeCallback 方法中,您可以通过设置 $this->shouldHandle = false 来阻止处理回调。不建议使用 beforeCallback 记录回调,因为它在执行任何验证之前执行。

解析常见暂停原因 parseCommonSuspensionReason 方法接受以下参数

  • reason: 来自 MyOwnFreeHost 的原始暂停原因。如果原因是与每日暂停无关,则返回 null,或者在简短形式中返回原因本身(例如,DAILY_HIT、DAILY_CPU、DAILY_IO、DAILY_EP)。

示例

use PlanetTheCloud\MofhCallbackClient\Callback;

// Create a new callback handler.
$callback = Callback::create([
    'ip' => '::1' // MyOwnFreeHost IP / Allowed caller IP address
]);

// Function to be executed when an account has been successfully activated
$callback->onAccountActivated(function ($username) {
    echo "Account successfully activated: {$username}";
});

// Function to be executed when an account has been suspended
$callback->onAccountSuspended(function ($username, $reason, ..., $common_reason) {
    echo "Account {$username} has been suspended with the following reason: {$reason}";
    if ($common_reason) {
        $reason = str_replace(['DAILY_EP', 'DAILY_CPU', 'DAILY_HIT', 'DAILY_IO'], ['Entry Process', 'CPU Usage', 'Website Hits', 'Input/Output'], $common_reason);
    }
    echo "Your account has been suspended because the daily {$reason} quota has been exhausted";
});

// Function to be executed when an account has been reactivated
$callback->onAccountReactivated(function ($username) {
    echo "Account {$username} has been reactivated";
});

// Function to be executed when SQL cluster callback is received
$callback->onSqlServer(function ($username, $cluster) {
    echo "Account {$username} has been moved to the {$cluster} cluster";
});

// Function to be executed when an account has been deleted
$callback->onAccountDeleted(function ($username) {
    echo "Account {$username} has been deleted";
});

// Function to be executed before the callback is handled
$callback->beforeCallback(function ($data, $ip) {
    // Do something before the callback is handled
    // Here are just an example
    if($data['status'] == 'SUSPENDED') {
        // This will skip handling of any callback with status SUSPENDED
        $this->shouldHandle = false;
    }
});

// Function to be executed after the callback is handled
$callback->afterCallback(function ($data, $ip) {
    file_put_contents('/tmp/mofh-callback-client.log', json_encode($data) . PHP_EOL, FILE_APPEND);
    echo "Callback has been logged to file";
});

// Wrap in a try-catch block. See Exception section for more information
try {
    $callback->handle($_POST);
} catch (\Exception $e) {
    echo $e->getMessage();
}

异常

以下异常由 handle 方法抛出

  • InvalidCallbackParameters: 当回调数据无效时抛出。
  • IpAddressMismatched: 当调用者的 IP 地址与在 create 方法中给出的允许 IP 地址不匹配时抛出。

支持

为可能存在于库中的错误/错误提供支持。
如果您寻求编程支持,这不是您要找的地方。
Join our Discord Server

许可证

版权 2022 PlanetTheCloud

根据 Apache 许可证版本 2.0("许可证")许可;除非遵守许可证,否则不得使用此文件。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可下分发的软件按“现状”分发,不提供任何形式的保证或条件,无论是明示的还是默示的。请参阅许可证了解管理许可权限和限制的特定语言。