開源 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註冊商原始解析後嘅 payload(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 { /* ... */ }
}