本文为您演示,如何通过阿里云CLI、OpenAPI Explorer和阿里云SDK等开发者工具调用DNS API。

使用阿里云CLI调用API示例

调用API前,请确保您已安装并配置了阿里云CLI。

本示例通过阿里云CLI调用API,完成添加解析记录(AddDomainRecord)、修改解析记录(UpdateDomainRecord)、获取解析记录列表(DescribeDomainRecords)以及获取子域名解析记录列表(DescribeSubDomainRecords)的任务。您可以根据API文档了解使用说明,并查询必需的请求参数。调用API报错时,您可以在相应API文档中获取排查建议。

  1. 使用 AddDomainRecord 添加解析记录 。
  2. 通过 UpdateDomainRecord 修改解析记录。示例将主机记录从apitest3修改为apitest4。
  3. 通过 DescribeDomainRecords 获取ddnstest.top的所有解析记录列表。
  4. 通过 DescribeSubDomainRecords 获取子域名apitest.dns-example.com 的所有解析记录列表。
  5. 通过 DescribeDomainRecordInfo 获取子域名的解析记录信息。

使用OpenAPI Explorer调用API示例

本示例通过阿里云OpenAPI Explorer,完成添加解析记录(AddDomainRecord)的任务。更多有关OpenAPI Explorer的详情,请参见什么是OpenAPI Explorer

云解析DNS调用TOP5的接口:

  1. 通过 AddDomainRecord 根据传入参数添加解析记录。
  2. 通过 UpdateDomainRecord 根据传入参数修改解析记录。
  3. 通过 DescribeDomainRecords 根据传入参数获取指定主域名的所有解析记录列表。
  4. 通过 DescribeSubDomainRecords 根据传入参数获取某个固定子域名的所有解析记录列表。
  5. 通过 DescribeDomainRecordInfo 根据传入参数获取某个固定子域名下的解析记录信息。

AddDomainRecord的调用测试如图:

OpenAPI Exploer调用调用结果

使用Java调用API示例

本示例通过Alibaba Cloud SDK for Java,完成添加解析记录(AddDomainRecord)的任务。关于如何配置DNS SDK,请参见SDK安装文档。

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.alidns.model.v20150109.*;

public class AddDomainRecord {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        AddDomainRecordRequest request = new AddDomainRecordRequest();
        request.setValue("3.0.3.0");
        request.setType("A");
        request.setRR("apitest");
        request.setDomainName("dns-example.com");

        try {
            AddDomainRecordResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}     

使用Node.js调用API示例

本示例通过Alibaba Cloud SDK for Node.js,完成添加解析记录(AddDomainRecord)的任务。

const Core = require('@alicloud/pop-core');

var client = new Core({
  accessKeyId: '<accessKeyId>',
  accessKeySecret: '<accessSecret>',
  endpoint: 'https://alidns.aliyuncs.com',
  apiVersion: '2015-01-09'
});

var params = {
  "DomainName": "dns-example.com",
  "RR": "apitest1",
  "Type": "A",
  "Value": "3.0.3.0"
}

var requestOption = {
  method: 'POST'
};

client.request('AddDomainRecord', params, requestOption).then((result) => {
  console.log(JSON.stringify(result));
}, (ex) => {
  console.log(ex);
})     

使用Go调用API示例

本示例通过Alibaba Cloud SDK for Go,完成添加解析记录(AddDomainRecord)的任务。

package main

import (
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
)

func main() {
    client, err := alidns.NewClientWithAccessKey("cn-hangzhou", "<accessKeyId>", "<accessSecret>")

    request := alidns.CreateAddDomainRecordRequest()
    request.Scheme = "https"

    request.Value = "3.0.3.0"
    request.Type = "A"
    request.RR = "apitest1"
    request.DomainName = "dns-example.com"

    response, err := client.AddDomainRecord(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf("response is %#v\n", response)
}  

使用PHP调用API示例

本示例通过Alibaba Cloud SDK for PHP,完成添加解析记录(AddDomainRecord)的任务。

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('cn-hangzhou') // replace regionId as you need
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::rpc()
                          ->product('Alidns')
                          // ->scheme('https') // https | http
                          ->version('2015-01-09')
                          ->action('AddDomainRecord')
                          ->method('POST')
                          ->options([
                                        'query' => [
                                          'DomainName' => "dns-example.com",
                                          'RR' => "apitest1",
                                          'Type' => "A",
                                          'Value' => "3.0.3.0",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}       

使用Python调用API示例

本示例通过Alibaba Cloud SDK for Python,完成添加解析记录(AddDomainRecord)的任务。

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest

client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = AddDomainRecordRequest()
request.set_accept_format('json')

request.set_Value("3.0.3.0")
request.set_Type("A")
request.set_RR("apitest1")
request.set_DomainName("dns-example.com")

response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))     

使用.net调用API示例

本示例通过Alibaba Cloud SDK for .net,完成添加解析记录(AddDomainRecord)的任务。

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Alidns.Model.V20150109;

namespace AlidnsDemo
{
    class Program{
        static void Main(string[] args){
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            var request = new AddDomainRecordRequest();
            request.Value = "3.0.3.0";
            request.Type = "A";
            request.RR = "apitest1";
            request.DomainName = "dns-example.com";
            try {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e){
                Console.WriteLine(e);
            }
            catch (ClientException e){
                Console.WriteLine(e);
            }
        }
    }
}      

使用Ruby调用API示例

本示例通过Alibaba Cloud SDK for Ruby,完成添加解析记录(AddDomainRecord)的任务。

# gem install aliyunsdkcore

require 'aliyunsdkcore'

client = RPCClient.new(
  access_key_id:     '<accessKeyId>',
  access_key_secret: '<accessSecret>',
  endpoint: 'https://alidns.aliyuncs.com',
  api_version: '2015-01-09'
)

response = client.request(
  action: 'AddDomainRecord',
  params: {
    "DomainName": "dns-example.com",
    "RR": "apitest1",
    "Type": "A",
    "Value": "3.0.3.0"
},
  opts: {
    method: 'POST'
  }
)

print response