无限星辰工作室-客户无限互联网动力之源
标题:
阿里云ddns 动态域名解析运行脚本
[打印本页]
作者:
crx349
时间:
2021-2-7 17:26
标题:
阿里云ddns 动态域名解析运行脚本
1.切换composer源
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
复制代码
2.安装组件
composer require alibabacloud/sdk
复制代码
3.ddnns.php
<?php
chdir(dirname(__FILE__));
//var_dump(dirname(__FILE__));exit;
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 = ['www.xmspace.net']; //域名列表,支持子域名
$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(dirname(__FILE__).'/ip'))
return 0;
else
file_put_contents(dirname(__FILE__).'/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(dirname(__FILE__).'/log.txt', json_encode($res) . PHP_EOL, FILE_APPEND);
}
}
}
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
} catch (\Throwable $e){
echo $e->getErrorMessage() . PHP_EOL;
file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
}
复制代码
4.设置计划任务 每小时执行一次
php /www/ddns/ddns.php
复制代码
欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://xmspace.net/)
Powered by Discuz! X3.4