开源 PHP

Registrar API

一个 PHP 库即可在 NameSilo、GoDaddy、Namecheap 和 Dynadot 之间管理域名——另加一个用于区域、DNS 和缓存的 Cloudflare 适配器。可用性查询、注册、续费、转移、DNS 记录和域名服务器,全部通过统一一致的接口完成。

composer require josuamarcelc/registrar-api

适配器

支持的注册商

通过品牌字符串选择适配器。所有适配器共享相同的方法签名,因此你无需重写代码即可切换注册商。

NameSilo'namesilo'
GoDaddy'godaddy'
Namecheap'namecheap'
Dynadot'dynadot'
CloudflareDNS / zones

在线演示

查询域名可用性

在服务器端通过 NameSilo 适配器发起真实的 checkAvailability() 调用。最多输入 10 个域名,用逗号、空格或换行分隔。每 5 分钟限查 10 次。

快速开始

三行代码即可调用

<?php
require __DIR__ . '/vendor/autoload.php';

use RegistrarAPI\RegistrarAPI;

$api = RegistrarAPI::make('namesilo', [
  'api_key' => 'YOUR_NAMESILO_API_KEY'
]);

$result = $api->checkAvailability(['example.com', 'mybrand.io']);
print_r($result);

凭据

各适配器配置

每个适配器接收一个配置数组。下面是通常所需的最小配置。

NameSilo
RegistrarAPI::make('namesilo', [
  'api_key' => 'YOUR_KEY',
]);
GoDaddy
RegistrarAPI::make('godaddy', [
  'api_key'    => 'KEY',
  'api_secret' => 'SECRET',
  // optional: 'base' => 'https://api.godaddy.com/v1'
]);
Namecheap
RegistrarAPI::make('namecheap', [
  'api_user'  => 'USERNAME',
  'api_key'   => 'KEY',
  'client_ip' => 'SERVER_PUBLIC_IP',
  // optional: 'base' => 'https://api.namecheap.com/xml.response'
]);
Dynadot
RegistrarAPI::make('dynadot', [
  'api_key' => 'KEY',
]);

方法

常用操作

每个适配器都实现相同的方法签名。返回值为规范化的关联数组。

// Availability
$api->checkAvailability(['example.com']);

// Lifecycle
$api->registerDomain('example.com', [
  'years'      => 1,
  'privacy'    => true,
  'auto_renew' => true,
  'registrant' => [ /* contact fields */ ],
]);
$api->renewDomain('example.com', 1);
$api->transferDomain('example.com', ['auth_code' => 'EPP']);
$api->getDomain('example.com');

// DNS records
$api->getDNS('example.com');
$api->setDNS('example.com', [
  ['type'=>'A',    'host'=>'@',   'value'=>'203.0.113.10', 'ttl'=>600],
  ['type'=>'CNAME','host'=>'www', 'value'=>'@',            'ttl'=>600],
]);
$api->addDNS('example.com', ['type'=>'TXT','host'=>'@','value'=>'v=spf1 -all','ttl'=>300]);
$api->delDNS('example.com', ['type'=>'TXT','host'=>'@']);

// Nameservers
$api->setNameServers('example.com', ['ns1.host.com','ns2.host.com']);

// Escape hatch — call any raw endpoint the adapter doesn't wrap
$api->raw('domains/suggestions?query=mybrand&limit=5');

响应结构

你会得到什么

okbool — 快速判断是否成功
raw注册商原始解析后的载荷(JSON 或 XML 转数组)
http可用时返回 HTTP 状态码
err传输层错误字符串(仅在调用失败时出现)
available / unavailable / invalidcheckAvailability() 填充的分组
recordsgetDNS() 返回的 {type,host,value,ttl,prio?} 数组

附赠

Cloudflare 适配器

同一个库、同样的结构,但用于区域、DNS 记录、缓存清除以及免费开关(Always HTTPS、Auto HTTPS Rewrites、Brotli、Minify、Rocket Loader、Dev Mode)。

$cf = RegistrarAPI::make('cloudflare', ['api_token' => 'CF_TOKEN']);
$cf->createZone('example.com');
$cf->setDNS('example.com', [
  ['type'=>'A','host'=>'@','value'=>'198.51.100.10','ttl'=>300,'proxied'=>true],
]);
$cf->purgeEverything('example.com');

贡献

在一个文件里添加新的注册商

src/adapters/ 中创建一个继承 BaseAdapter 的类,并实现其抽象方法。Composer 会自动加载它。

namespace RegistrarAPI\Adapters;

use RegistrarAPI\Core\BaseAdapter;

class MyRegistrar extends BaseAdapter {
  protected string $brand = 'myregistrar';

  public function checkAvailability(array $domains): array { /* ... */ }
  public function registerDomain(string $domain, array $opts): array { /* ... */ }
  public function renewDomain(string $domain, int $years=1, array $opts=[]): array { /* ... */ }
  public function transferDomain(string $domain, array $opts): array { /* ... */ }
  public function getDomain(string $domain): array { /* ... */ }
  public function getDNS(string $domain): array { /* ... */ }
  public function setDNS(string $domain, array $records): array { /* ... */ }
  public function addDNS(string $domain, array $record): array { /* ... */ }
  public function delDNS(string $domain, array $selector): array { /* ... */ }
  public function setNameServers(string $domain, array $nameservers): array { /* ... */ }
  public function raw(string $op, array $params=[]): array { /* ... */ }
}