一、接口简介
这是接口盒子提供的一个免费 域名WHOIS 信息实时查询接口,覆盖超过 1000+ 域名后缀,包括常见的 .com、.net、.cn、.org等顶级域名和国别域名。
接口返回的是原始 WHOIS 信息内容,开发者可以根据自己的需求从中提取注册人、注册商、注册时间、到期时间、DNS 服务器等关键字段。
💡 适用场景:域名归属查询、域名监控、到期提醒、品牌保护、站长工具、批量域名信息采集等。
二、请求地址与请求方式
- 请求地址:
https://cn.apihz.cn/api/wangzhan/whoisall.php - 请求方式:GET 或 POST 均可
- 请求编码:UTF-8(带中文参数时必须使用 UTF-8,否则可能请求失败)
三、请求参数说明
参数 |
名称 |
必填 |
说明 |
id |
用户 ID |
是 |
用户中心的数字 ID,例如 |
key |
用户 KEY |
是 |
用户中心通讯秘钥,例如 |
domain |
查询域名 |
是 |
要查询的域名,尽量使用主域名,不要带 http(s),例如 |
type |
查询方式 |
否 |
|
四、返回参数说明
接口返回 JSON 格式数据,主要包含以下字段:
参数 |
名称 |
说明 |
code |
状态码 |
|
msg |
信息提示 |
成功或失败的具体提示信息 |
domain |
查询域名 |
当前查询的域名 |
whois / data |
WHOIS 信息 |
原始 WHOIS 内容,不同注册局格式不同,需自行提取;换行符统一为 |
成功返回示例
{ "code": 200, "domain": "apihz.cn", "whois": "Domain Name: apihz.cn\nROID: 20240319s10001s56425741-cn\nDomain Status: clientUpdateProhibited\nDomain Status: clientTransferProhibited\nRegistrant: 绵阳耳关明皿网络科技有限公司\nRegistrant Contact Email: erguanmingmin@qq.com\nSponsoring Registrar: 腾讯云计算(北京)有限责任公司\nName Server: ns3.dnsv2.com\nName Server: ns4.dnsv2.com\nRegistration Time: 2024-03-19 01:34:31\nExpiration Time: 2028-03-19 01:34:31\nDNSSEC: signedDelegation\n" }
失败返回示例
{ "code": 400, "msg": "通讯秘钥错误。" }
五、GET 请求示例(官方原版)
直接在浏览器或 HTTP 客户端中请求:
https://接口盒子/api/wangzhan/whoisall.php?id=88888888&key=88888888&domain=apihz.cn&type=1
六、POST 请求示例(官方说明)
官方文档中说明 POST 请根据开发语言自行实现,下面分别给出 PHP 和 Python 的完整示例。
七、PHP 调用示例
方式一:cURL GET 请求(推荐,最简单)
<?php $id = "10000000"; // 用户中心数字ID $key = "your_key_here"; // 通讯秘钥 $domain = "apihz.cn"; // 查询域名,不带 http $type = 1; // 1=缓存优先,2=直连注册局 // 构建请求URL $apiUrl = "https://接口盒子/api/wangzhan/whoisall.php"; $requestUrl = $apiUrl . "?" . http_build_query([ 'id' => $id, 'key' => $key, 'domain' => $domain, 'type' => $type ]); // 初始化cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $requestUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 本地测试可关闭,生产环境建议开启 $response = curl_exec($ch); // 错误处理 if (curl_errno($ch)) { die("cURL 请求错误:" . curl_error($ch)); } curl_close($ch); // 解析JSON $data = json_decode($response, true); if ($data['code'] == 200) { // 处理换行符 $whois = str_replace(["\\r\\n", "\\n", "\\r"], PHP_EOL, $data['whois'] ?? $data['data'] ?? ''); echo "查询域名:" . $data['domain'] . PHP_EOL; echo "WHOIS 信息:" . PHP_EOL; echo $whois . PHP_EOL; } else { echo "查询失败:" . $data['msg'] . PHP_EOL; }
方式二:cURL POST 请求
<?php $apiUrl = "https://接口盒子/api/wangzhan/whoisall.php"; $params = [ 'id' => '10000000', 'key' => 'your_key_here', 'domain' => 'apihz.cn', 'type' => 1 ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); if (curl_errno($ch)) { die("cURL 请求错误:" . curl_error($ch)); } curl_close($ch); $data = json_decode($response, true); if ($data['code'] == 200) { $whois = str_replace(["\\r\\n", "\\n"], PHP_EOL, $data['whois'] ?? $data['data'] ?? ''); echo "查询成功!" . PHP_EOL; echo $whois . PHP_EOL; } else { echo "错误:" . $data['msg'] . PHP_EOL; }
八、Python 调用示例
方式一:requests GET 请求(推荐)
import requests def query_whois(domain, uid, ukey, query_type=1): url = "https://接口盒子/api/wangzhan/whoisall.php" params = { "id": uid, "key": ukey, "domain": domain, "type": query_type } try: response = requests.get(url, params=params, timeout=30) response.raise_for_status() result = response.json() if result.get("code") == 200: # 处理换行符 whois_raw = result.get("whois") or result.get("data", "") whois_data = whois_raw.replace("\\r\\n", "\n").replace("\\n", "\n") return { "success": True, "domain": result.get("domain"), "whois": whois_data } else: return { "success": False, "msg": result.get("msg", "未知错误") } except requests.exceptions.Timeout: return {"success": False, "msg": "请求超时"} except requests.exceptions.RequestException as e: return {"success": False, "msg": f"网络请求异常:{e}"} except ValueError: return {"success": False, "msg": "返回数据不是合法 JSON"} if __name__ == "__main__": # 替换为自己的 ID 和 KEY UID = "10000000" UKEY = "your_key_here" res = query_whois("apihz.cn", UID, UKEY, query_type=1) if res["success"]: print("查询域名:", res["domain"]) print("WHOIS 信息:") print(res["whois"]) else: print("查询失败:", res["msg"])
方式二:requests POST 请求
import requests url = "https://接口盒子/api/wangzhan/whoisall.php" payload = { "id": "10000000", "key": "your_key_here", "domain": "apihz.cn", "type": 1 } try: response = requests.post(url, data=payload, timeout=30) response.raise_for_status() result = response.json() if result["code"] == 200: whois = result.get("whois") or result.get("data", "") whois = whois.replace("\\r\\n", "\n").replace("\\n", "\n") print("查询成功!") print(whois) else: print("查询失败:", result["msg"]) except requests.exceptions.RequestException as e: print("请求异常:", e)
九、如何解析原始 WHOIS 数据
因为不同注册局的 WHOIS 格式不一样,接口返回的是原始信息,通常需要自己提取关键字段。这里给一个 Python 简单解析示例:
import re def parse_whois(raw): info = {} for line in raw.split("\n"): if ":" in line: k, v = line.split(":", 1) info[k.strip()] = v.strip() # 常见字段提取 print("注册人:", info.get("Registrant", "")) print("注册商:", info.get("Sponsoring Registrar", "")) print("注册时间:", info.get("Registration Time", "")) print("到期时间:", info.get("Expiration Time", "")) print("DNS 服务器:", [v for k, v in info.items() if "Name Server" in k]) # 在拿到 whois 字符串后调用 # parse_whois(whois)
十、常见问题与注意事项
- 编码问题:请求必须使用 UTF-8,否则带中文的域名或参数可能失败。
- type 参数选择:
type=1:优先缓存,速度快,适合批量查询;type=2:直连注册局,实时性高但慢,适合精确校验。
- 换行符处理:返回数据中的换行符可能是
\n、\r\n或\\r\\n,需要根据语言做替换处理。 - 字段差异:不同后缀、不同注册局的 WHOIS 格式不完全一致,提取注册人、邮箱、到期时间时需要做兼容处理。
- 异常处理:WHOIS 查询受上游注册局影响,可能出现超时、限流、无响应等情况,代码中一定要加超时和异常捕获。
十一、总结
这个接口最大的优势是免费、支持后缀多、每日无调用上限,非常适合个人开发者、站长工具、域名监控系统等轻量级项目接入。只要注册账号拿到自己的 ID 和 KEY,再用上面的 PHP 或 Python 示例稍作修改,就能快速把全球域名 WHOIS 查询能力集成到自己的系统里。