returnearly/dnsmadeeasy

DNS Made Easy API 的 PHP SDK

v2.0.0 2022-11-27 05:09 UTC

This package is auto-updated.

Last update: 2024-08-27 09:04:50 UTC


README

这是一个 DNS Made Easy API 的客户端库。

有关 API 的更多信息可以在官方 API 文档中找到。

安装

安装和使用此客户端库最简单的方法是使用 Composer。以下命令将库添加到您的应用程序并从 Packagist 安装它。

composer require returnearly/dnsmadeeasy

入门

您需要一个 DNS Made Easy 账户和 API 凭证。您可以在 DNS Made Easy 网站 上获取账户。有一个 API 沙盒可用,您可以在这里创建新账户。

如果您正在使用 Composer,则应运行 Composer 的自动加载以加载库

require_once 'vendor/_autoload.php';

加载库后,您只需创建客户端并设置 API 密钥和密钥。

$client = new \DnsMadeEasy\Client;
$client->setApiKey(API_KEY);
$client->setSecretKey(SECRET_KEY);

现在您可以使用客户端查询 API 并检索对象。使用方法在 GitHub 的 docs 目录中有所记录。

使用沙盒

您可以使用 setEndpoint 方法告诉客户端使用沙盒 API 端点。

$client->setEndpoint('https://api.sandbox.dnsmadeeasy.com/V2.0');

综合起来

让我们来个 API 的“Hello World”。让我们获取您的域列表。

<?php
// Load the library and dependencies
require_once 'vendor/_autoload.php';

// Create a new client and set our credentials
$client = new \DnsMadeEasy\Client;
$client->setApiKey("Your API Key");
$client->setSecretKey("Your Secret Key");

// Configure it to use the Sandbox
$client->setEndpoint('https://api.sandbox.dnsmadeeasy.com/V2.0');

// Create a new domain
$domain = $client->domains->create();
$domain->name = 'mydomain.example.com';
$domain->save();

// Print out our domain
echo json_encode($domain, JSON_PRETTY_PRINT);

// Now fetch a list of our domains
$domains = $client->domains->paginate();
foreach ($domains as $domain) {
    echo json_encode($domain, JSON_PRETTY_PRINT);
}

下面还有更多使用 API 客户端 SDK 的示例。

配置

您还可以使用客户端使用额外的配置选项,而不仅仅是指定沙盒。

日志记录

您可以选择实现 PSR-3 Logger 规范的记录器,例如 MonoLog。客户端是 LoggerAwareInterface,记录器可以在构造函数或方法调用中指定。

$client = new \DnsMadeEasy\Client(null, null, $myLogger);
$client->setLogger($myLogger);

如果没有指定记录器,则将使用一个不执行任何操作的空记录器。

自定义 HTTP 客户端

如果您需要为您的应用程序中的 HTTP 请求进行额外配置,例如指定代理服务器或如果您想使用符合 PSR-18 HTTP Client 规范的自定义 HTTP 客户端。

您可以使用构造函数或方法调用指定客户端。

$client = new \DnsMadeEasy\Client($myClient);
$client->setHttpClient($myClient);

示例

库方法的完整文档在 docs 文件夹中。

管理器

管理器用于管理您对 API 资源的访问,包括在 API 中创建新资源并检索现有资源。这些可以通过客户端上的属性访问。

// Fetch our manager
$domainsManager = $client->domains;
// Ask our manager for the domain
$domain = $domainsManager->get(1234);

管理器还用于创建新对象。

// Create a new domain
$domain = $client->domains->create();
$domain->name = 'example.com';

// Save the domain
$domain->save();

在您调用 $domain->save() 之前,域不会在 API 中保存。

您可以使用管理器上的 paginate() 方法检索多个对象。您可以指定页码和每页项目数。

// Return the 4th page of results with the default page size
$client->domains->paginate(4);

// Return the first page of 50 results
$client->domains->paginate(1, 50);

模型

模型本身遵循 Active Record 模式。可以更新属性并在模型上调用 save() 以更新 API。

// Fetch an existing domain with the ID 1234
$domain = $client->domains->get(1234);
// Update the gtdEnabled property
$domain->gtdEnabled = true;
// Save the domain object on the API
$domain->save();

您可以通过在它上调用 delete() 来删除对象

$domain = $client->domains->get(1234);
$domain->delete();

创建域和记录

此示例创建了一个新的域并向其中添加记录。

// Include composer libraries
require_once 'vendor/_autoload.php';

// Create the client
$client = new \DnsMadeEasy\Client;
$client->setApiKey(API_KEY);
$client->setSecretKey(SECRET_KEY);

// Create the domain
$domain = $client->domains->create();
$domain->name = 'example.com';
$domain->save();

// Create a record on the domain
$record = $domain->records->create();
$record->type = \DnsMadeEasy\Enums\RecordType::A();
$record->name = 'www';
$record->value = '192.0.2.1';
$record->save();

// Get a list of all domains
$domains = $client->domains->paginate();
foreach ($domains as $domain) {
    print_r(json_encode($domain, JSON_PRETTY_PRINT));
}

许可证

MIT 许可证 (MIT)

版权所有 (c) 2020 DNS Made Easy,Tiggee LLC 的子公司。

在此特此授予任何人获得此软件及其相关文档文件(以下简称“软件”)副本的权限,免费使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许将软件提供给他人进行上述操作,但须遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的还是默示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任承担责任,无论这些责任是因合同行为、侵权或其他行为引起的,无论是在软件或软件的使用或其他与软件相关的活动中产生的。