bicisteadm/subreg-api-php

Subreg.cz API的PHP客户端

dev-master 2022-02-24 09:41 UTC

This package is auto-updated.

Last update: 2024-09-24 15:17:21 UTC


README

用于简单和安全访问Subreg.cz API的包。

关于Subreg API

安装

通过Composer安装

composer install bicisteadm/subreg-api-php

它需要PHP版本7.3,并支持PHP 7.4。

访问级别

您可以使用两种方式使用此包

  • 原始访问:只需调用原始命令并获取原始响应 - 该包仅处理请求身份验证、连接或服务器错误,并检查基本信封或响应数据。
  • 上下文访问:完全服务访问,允许遍历数据,通过严格模式检查请求和响应数据,通过严格类型获取器访问所有属性。

更多详细信息请参阅下面的文档。

身份验证与环境

身份验证

对于API的身份验证,请使用Credentials

$credentials = new \Redbitcz\SubregApi\Credentials('microsoft', 'password');

如果您使用Subreg管理员访问帐户,请使用'#'将用户名和管理员名称连接起来

$credentials = new \Redbitcz\SubregApi\Credentials('gates#microsoft', 'password');

有关管理员访问的更多信息,请访问Subreg API登录文档页面

实时/测试环境

该包处理所有对生产环境的请求。

如果您需要测试应用程序的测试环境,您可以更改URL端点

$credentials = new \Redbitcz\SubregApi\Credentials('microsoft', 'password', 'https://ote-soap.subreg.cz/cmd.php');

使用原始访问

使用Credentials对象创建Client

$client = new \Redbitcz\SubregApi\Client($credentials);

Client只有一个方法:$client->call(),用于向Subreg.cz API发送已验证的请求。

$client->call('Check_Domain', ['domain' => 'subreg.cz'])->getData();

/*
    Returns the Array:

    [
      'name' => 'subreg.cz',
      'avail' => 0,
      'price' => 
      [
        'amount' => 165.0,
        'amount_with_trustee' => 165.0,
        'premium' => 0,
        'currency' => 'CZK',
      ],
    ]
*/

错误

Client可以在多个级别上失败。

  • 无效请求 - 当您尝试提交无效请求时,客户端抛出InvalidRequestException
  • 连接问题 - 离线、无效URL、服务器问题等,客户端抛出ConnectionException
  • 服务器响应错误 - API服务器正在运行,但不能完成您的请求,域名不存在等,客户端抛出ResponseErrorException
  • 响应的其他问题 - 响应缺少所需的字段等,客户端抛出InvalidResponseException

ResponseErrorException(及其所有子类异常)包含整个响应,使用

$exception->getResponse();

Response

响应被封装在Response信封中,以便更好地处理。响应始终包含一个数组。

getData(): array

从API服务器返回所有数据作为响应。

getItem(string $name)

从响应数组返回一个项目。如果项目不存在,则返回null

它仅允许访问响应数组的第一个级别(不允许递归访问深层结构化数据)。

hasItem(string $name): bool

当项目存在于响应中时返回true,否则返回false

getMandatoryItem(string $name)

从响应数组返回一个项目。如果项目不存在,则抛出InvalidResponseException

工厂快捷方式

创建Client实例需要首先创建Credentials

$credentials = new \Redbitcz\SubregApi\Credentials('microsoft', 'password');
$client = new \Redbitcz\SubregApi\Client($credentials);

这可以通过Factory辅助器简化

$client = \Redbitcz\SubregApi\Factory::createClient('microsoft', 'password');

如果您使用管理员访问帐户,请使用

$client = \Redbitcz\SubregApi\Factory::createClientForAdministrator('microsoft', 'gates', 'password');

示例

$client = \Redbitcz\SubregApi\Factory::createClient('microsoft', 'password');

$response = $client->call('Info_Domain', ['domain' => 'subreg.cz']);

echo "Domain {$response->getItem('name')} is expiring at {$response->getItem('exDate')}.";
// Domain subreg.cz is expiring at 2023-04-22.

使用上下文访问

首先创建Context对象实例。使用Factory辅助器简单创建它

$context = \Redbitcz\SubregApi\Factory::createContext('microsoft', 'password');

使用Context对象,您可以通过类似流畅的属性轻松访问API资源。

foreach ($context->domain()->list() as $domain) {
    echo "Domain {$domain->getName()} is expiring at {$domain->getExpire()->format('Y-m-d')}.\n";
}
// Domain my-first-domain.cz is expiring at 2023-04-22.
// Domain my-second-domain.cz is expiring at 2020-01-01.
// Domain my-third-domain.cz is expiring at 2021-08-30.
// Domain my-fourth-domain.cz is expiring at 2022-01-15.
// Domain my-fifth-domain.cz is expiring at 2020-05-13.
// Domain my-sixth-domain.cz is expiring at 2020-05-13.

注意:上下文访问仅覆盖了Subreg.cz API命令中的一些最常用的命令。Subreg.cz API命令手册

正在进行中

该软件包仍在开发中。

未来的示例在视野中

批量续费即将到期的域名

$expiringDomains = $context->domain()->list()->filter(['expire < ' => new DateTime('+ 1 month')]); 

foreach ($expiringDomains as $domain) {
    $order = $domain->renew(1); // 1 year
    echo "Domain {$domain->getName()} is renewed by order ID: {$order->getId()}.\n";
}
// Domain my-second-domain.cz is renewed by order ID: 12345000.
// Domain my-fifth-domain.cz is renewed by order ID: 12345001.
// Domain my-sixth-domain.cz is renewed by order ID: 12345002.

从3级以上域名的区域中删除已弃用的SPF记录

foreach ($context->domain()->list() as $domain) {
    foreach ($domain->dns()->list()->filter(['!isType' => 'SPF', '@getFqn() ~ ' => '/\w+\.\w+\.\w+$/']) as $dnsRecord) {
        $dnsRecord->delete();
        echo "Removed SPF record for {$dnsRecord->getFqn()}.\n";
    }
}
// Removed SPF record for subdomain.my-first-domain.cz.
// Removed SPF record for cluster-1.region-eu.my-thitd-domain.cz.