构建PHP接口,更新阿里云DNS解析

使用http get的方式获取ip地址,然后使用阿里云的dns接口,更新dns解析记录。

示例代码如下:后续的安全逻辑和执行规则,请自行补充完成,本站概不负责😄

<?php

require 'vendor/autoload.php';
include "conf.php";//配置文件定义httpget参数及token算法

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Exception\ClientException;



if($token==$set_token )
    {
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
            ->regionId($regionId)
            ->asDefaultClient();
        try {
            // 发送请求查询域名解析记录
            $result = AlibabaCloud::rpc()
                                  ->product('Alidns')
                                  ->version('2015-01-09')
                                  ->action('DescribeDomainRecords')
                                  ->method('POST')
                                  ->host('alidns.aliyuncs.com')
                                  ->options([
                                      'query' => [
                                          'DomainName' => $DomainName,
                                          'RRKeyWord' => $RRKeyWord,
                                          'TypeKeyWord' => 'A', // 查询A记录
                                      ],
                                  ])
                                  ->request();
            // 解析响应并获取 recordid
            $recordId = null;
            $records = $result['DomainRecords']['Record'];
            foreach ($records as $record) {
                if ($record['RR'] === $RRKeyWord) {
                    $recordId = $record['RecordId'];
                    break;
                }
            }
            // 检查是否获取到 recordid
            if ($recordId) {
                // 使用获取的 recordid 更新解析记录值为 1.1.1.1
                $updateResult = AlibabaCloud::rpc()
                                            ->product('Alidns')
                                            ->version('2015-01-09')
                                            ->action('UpdateDomainRecord')
                                            ->method('POST')
                                            ->host('alidns.aliyuncs.com')
                                            ->options([
                                                'query' => [
                                                    'RecordId' => $recordId,
                                                    'RR' => $RRKeyWord,
                                                    'Type' => 'A',
                                                    'Value' => $Domain_Value,
                                                ],
                                            ])
                                            ->request();
                //echo "Update Result: " . json_encode($updateResult) . PHP_EOL;
            } else {
                exit;
                //echo "No matching record found for url" . PHP_EOL;
            }
        
        } catch (ClientException $e) {
            echo "ClientException: " . $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo "ServerException: " . $e->getErrorMessage() . PHP_EOL;
        }
        echo 'in';
    }
else
    {
        echo 'out';
        exit;
    }
 

需要加载的public_api_conf.php如下:

<?php
//--------------------------------------------------------------------------
//公用参数
date_default_timezone_set('PRC');//时区
$time_now      = date("Y-m-d G:i:s",time());//格式化时间
$token         = $_GET['token'];//自定义算法删除了,可以自己写,这里直接定义的http get直接传参。
//--------------------------------------------------------------------------
//阿里云公用参数
$secret        = $_GET['secret'];//推送消息参数定位
$accessKeyId = 'Access Key ID'; // 替换为您的 Access Key ID
$accessKeySecret = 'Access Key'; // 替换为您的 Access Key Secret
$regionId = 'cn-hangzhou'; // 选择合适的区域
//--------------------------------------------------------------------------
//阿里云域名参数
$RRKeyWord     =$_GET['keyword'];
$DomainName    =$_GET['domain'];
$Domain_Value  =$_GET['ip'];
未经允许不得转载:阡陌 » 构建PHP接口,更新阿里云DNS解析

相关文章

contact