好物优选点击查看详情 京东购买

暂无内容

阿里云域名动态解析dns,ddns php定时更新

keyword:阿里云域名动态解析dns,ddns,不固定ip动态解析,自动域名解析,域名解析到动态ip

电信宽带,打电话给来了公网ip但是不固定,内网服务器没有固定的公网IP,每隔两三天会变一次,手工修改太麻烦,故而想到使用阿里云的接口动态修改,代码使用PHP实现

1访问外网接口获取到服务器出口真实IP

2.调用阿里云解析DescribeSubDomainRecords接口获取到域名的RecordId

3.使用UpdateDomainRecord修改记录值

4.加入到定时任务,每隔一段时间执行下即可

阿里云sdk composer引入

composer require alibabacloud/sdk

ddns.php

<?php require './vendor/autoload.php'; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; $isLocalIP = true; // 本地远程地址true,请求地址false $accessKeyId = ''; // 你的accessKeyId 申请地址https://usercenter.console.aliyun.com/#/manage/ak $accessSecret = ''; // 你的accessSecret $domains = ['xxx.xxx.com']; //域名列表,支持子域名 $getIpUrl = 'http://ifconfig.me/ip';//获取出口IP接口地址 if ($isLocalIP) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $getIpUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $data = curl_exec($ch); curl_close($ch); $ip = $data; } else { $ip = $_SERVER['REMOTE_ADDR']; } try { //没有改动则不更新 if ($ip == file_get_contents('./ip')) return 0; else file_put_contents('./ip', $ip); AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret) ->regionId('cn-hangzhou')// replace regionId as you need ->asDefaultClient(); $request = AlibabaCloud::rpc() ->product('Alidns') ->scheme('http') ->version('2015-01-09') ->action('DescribeSubDomainRecords') ->method('POST') ->host('alidns.aliyuncs.com'); foreach ($domains as $domain) { $requestClone = clone $request; $result = $requestClone->options([ 'query' => [ 'RegionId' => "default", 'SubDomain' => $domain, ], ])->request(); $domainInfo = $result->toArray(); if (empty($domainInfo['DomainRecords']['Record'])) { return; } $records = $domainInfo['DomainRecords']['Record']; foreach ($records as $record) { if ($record['Type'] == 'A') { $recordId = $record['RecordId']; $domainArr = explode('.', $domain); $res = AlibabaCloud::rpc() ->product('Alidns') ->scheme('http') ->version('2015-01-09') ->action('UpdateDomainRecord') ->method('POST') ->host('alidns.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "default", 'RecordId' => $recordId, 'RR' => $domainArr[0], 'Type' => "A", 'Value' => $ip, ], ]) ->request(); file_put_contents('./log.txt', json_encode($res) . PHP_EOL, FILE_APPEND); } } } } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; file_put_contents('./log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND); } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; file_put_contents('./log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND); } catch (\Throwable $e){ echo $e->getErrorMessage() . PHP_EOL; file_put_contents('./log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);

将下面命令加到 定时任务里(宝塔,corntab等等)

php start.php

原文链接:https://blog.csdn.net/flysnownet/article/details/112137840?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168525635616782427442085%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168525635616782427442085&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-27-112137840-null-null.268%5Ev1%5Econtrol&utm_term=NAS%E3%80%81%E7%BE%A4%E6%99%96%E3%80%81%E9%98%BF%E9%87%8C%E4%BA%91%E3%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E3%80%81%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E3%80%81ipv6%E3%80%81ddns%E3%80%81%E8%BD%BB%E9%87%8F%E7%BA%A7%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E9%93%81%E5%A8%81%E9%A9%AC%E3%80%81%E5%A8%81%E8%81%94%E9%80%9A%E3%80%81DSM%E3%80%81DSM6.0%E3%80%81%E7%BE%A4%E6%99%96nas%E3%80%81%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E8%9C%97%E7%89%9B%E6%98%9F%E9%99%85%E3%80%81%E9%BB%91%E7%BE%A4%E6%99%96%E3%80%81docker%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81%E5%9F%9F%E5%90%8D%E6%B3%A8%E5%86%8C%E3%80%81%E5%AE%9D%E5%A1%94%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E3%80%81nginx%E3%80%81frp%E3%80%81%E5%8A%A8%E6%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享