如何在Node.js中调用Dreamscape API实现域名可用性查询
Node.js 实现Dreamscape API域名查询功能方案
你可以直接使用Node.js的SOAP客户端对接官方API,无需依赖不完整的第三方封装库,实现逻辑和你现有的PHP代码完全对齐,步骤如下:
1. 安装依赖
使用Node.js生态成熟的soap库处理SOAP请求:
npm install soap
2. 完整实现代码
const soap = require('soap'); // 配置参数 const SOAP_LOCATION = 'http://soap.secureapi.com.au/API-2.1'; const WSDL_LOCATION = 'http://soap.secureapi.com.au/wsdl/API-2.1.wsdl'; const RESELLER_ID = '替换为你的ResellerID'; const API_KEY = '替换为你的APIKey'; /** * 封装Dreamscape API调用方法,和PHP版逻辑对齐 * @param {string} method 调用的API方法名 * @param {object|null} data 请求参数 * @returns {Promise<object>} 接口返回结果 */ async function dreamScapeAPI(method, data = null) { // 构造认证头 const authenticate = { AuthenticateRequest: { ResellerID: RESELLER_ID, APIKey: API_KEY } }; const soapHeader = new soap.Header(SOAP_LOCATION, 'Authenticate', authenticate); // 创建SOAP客户端 const client = await soap.createClientAsync(WSDL_LOCATION, { soapVersion: soap.SOAP_1_2, disableCache: true }); client.addSoapHeader(soapHeader); // 调用接口 try { const [response] = await client[method + 'Async'](data || {}); return response; } catch (error) { return error; } } // 域名查询调用示例 async function checkDomainAvailability(targetDomain, domainNames) { let available = false; const altDomains = []; let apiError = null; const response = await dreamScapeAPI('DomainCheck', { DomainNames: domainNames }); // 不是SOAP错误则处理返回结果 if (!(response instanceof Error)) { if (response.APIResponse?.AvailabilityList) { // 兼容单/多域名返回的数组/对象格式 const availabilityList = Array.isArray(response.APIResponse.AvailabilityList) ? response.APIResponse.AvailabilityList : [response.APIResponse.AvailabilityList]; availabilityList.forEach(item => { if (item.Available) { if (item.Item === targetDomain) { available = true; } else { altDomains.push(item.Item); } } }); } } else { // 处理错误信息 if (response.APIResponse?.Errors) { const errors = Array.isArray(response.APIResponse.Errors) ? response.APIResponse.Errors : [response.APIResponse.Errors]; apiError = errors.map(e => e.Message).join(';'); } } return { available, altDomains, apiError }; } // 调用示例 // checkDomainAvailability('example.com', ['example.com', 'example.net', 'example.org']) // .then(res => console.log(res))
逻辑说明
- 认证逻辑、SOAP协议版本、WSDL地址和你PHP代码的配置完全一致,不会出现接口权限问题
- 返回结果处理逻辑和PHP版对齐,兼容单域名、多域名查询的返回格式
- 所有异步操作用async/await封装,符合Node.js开发习惯
- 你可以直接根据业务需求调整入参和返回值的处理规则
内容的提问来源于stack exchange,提问作者Shawn
相关产品推荐
相关产品推荐

