20i/api-modules

访问20i API的模块

dev-master 2018-10-05 12:32 UTC

This package is auto-updated.

Last update: 2024-09-06 10:27:33 UTC


README

简介

这是20i服务的REST模块。它提供了访问20i服务的必要包装器,无需编写自己的REST客户端。

需求

  • Composer
  • PHP5或更高版本。请注意,未来版本可能需要PHP7。

服务示例

以下示例中,服务的API密钥将给出为“API密钥”。您应将其替换为API页面上的实际值 https://my.20i.com/reseller/api

添加新的托管服务

<?php
$general_api_key = "API KEY";
$services_api = new \TwentyI\API\Services($general_api_key);
$type = "5678";
$domain_name = "example.org";
$other_domain_names = ["example.net"];

$response = $services_api->postWithFields(
    "/reseller/*/addWeb",
    [
      "type" => $type,
      "domain_name" => $domain_name,
      "extra_domain_names" => $other_domain_names,
    ]
);

在域名上设置名称服务器

<?php
$general_api_key = "API KEY";
$services_api = new \TwentyI\API\Services($general_api_key);

$domains = $services_api->getWithFields("/domain");
foreach ($domains as $domain) {
    if ($domain->name == "example.org") {
        $id = $domain->id;
        $old_nameservers = $services_api->getWithFields(
            "/domain/{$id}/nameservers"
        )->result;
        $services_api->postWithFields(
            "/domain/{$id}/nameservers",
            [
                "ns" => ["ns1.example.org", "ns2.example.org"],
                "old-ns" => $old_nameservers,
            ]
        );
    }
}

查找域名

<?php
$general_api_key = "API KEY";
$services_api = new \TwentyI\API\Services($general_api_key);
$domains = $services_api->getWithFields("/domain-search/mybusinessname");
print_r($domains);

身份验证示例

这些仅适用于您尝试编写自己的Stack控制面板版本的情况。

以下示例中,身份验证API客户端密钥将给出为“客户端密钥”。您应将其替换为API页面上的实际值 https://my.20i.com/reseller/api

使用用户名/密码验证用户

<?php
$oauth_client_key = "CLIENT KEY";
$username = "mycoolusername";
$password = "thatpasswordilike";

$auth_api = new \TwentyI\API\Authentication($oauth_client_key);
$response = $auth_api->postWithFields("/login/authenticate", [
    "grant_type" => "password",
    "username" => $username,
    "password" => $password,
]);
$new_access_token = $response->access_token;

以您拥有的用户身份进行验证

<?php
$oauth_client_key = "CLIENT KEY";
$subuser_reference = "stack-user:97";

$auth_api = new \TwentyI\API\Authentication($oauth_client_key);
$response = $auth_api->postWithFields("/login/authenticate", [
    "grant_type" => "client_credentials",
    "scope" => $subuser_reference,
]);
$new_access_token = $response->access_token;

其他示例

单点登录

$oauth_client_key = "CLIENT KEY";
$general_api_key = "API KEY";

$services_api = new \TwentyI\API\Services($general_api_key);
$auth_api = new \TwentyI\API\Authentication($oauth_client_key);


$all_packages = $services_api->getWithFields("/package");
$package_id = $all_packages[0]->id; // This is just your first package

$stack_users = $services_api->getWithFields("/package/{$package_id}/stackUserList");
$token_info = $auth_api->controlPanelTokenForUser(
    $stack_users[0]->identity
);
$url = $services_api->singleSignOn($token_info->access_token, $package_info->name);

注意事项

尽可能直接从EPP格式映射域名服务。这意味着当像Nominet这样的注册机构在其标签名称中使用连字符(例如 opt-out)时,它们最终会在结果对象中也变成连字符。您仍然可以使用它们,只需编写代码略有不同。例如,$company_number = $contact->extension->{'co-no'};

20iRestModule