无限星辰工作室-客户无限互联网动力之源

标题: 阿里云ddns 动态域名解析运行脚本 [打印本页]

作者: crx349    时间: 2021-2-7 17:26
标题: 阿里云ddns 动态域名解析运行脚本
1.切换composer源
  1. composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
复制代码

2.安装组件
  1. composer require alibabacloud/sdk
复制代码

3.ddnns.php
  1. <?php
  2. chdir(dirname(__FILE__));
  3. //var_dump(dirname(__FILE__));exit;
  4. require './vendor/autoload.php';

  5. use AlibabaCloud\Client\AlibabaCloud;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Exception\ServerException;

  8. $isLocalIP = true; // 本地远程地址true,请求地址false
  9. $accessKeyId = ''; // 你的accessKeyId 申请地址https://usercenter.console.aliyun.com/#/manage/ak
  10. $accessSecret = ''; // 你的accessSecret
  11. $domains = ['www.xmspace.net']; //域名列表,支持子域名
  12. $getIpUrl = 'http://ifconfig.me/ip';//获取出口IP接口地址

  13. if ($isLocalIP) {
  14.     $ch = curl_init();
  15.     $timeout = 5;
  16.     curl_setopt($ch, CURLOPT_URL, $getIpUrl);
  17.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  19.     $data = curl_exec($ch);
  20.     curl_close($ch);
  21.    $ip = $data;
  22. } else {
  23.     $ip = $_SERVER['REMOTE_ADDR'];
  24. }



  25. try {
  26.     //没有改动则不更新
  27.         if ($ip == file_get_contents(dirname(__FILE__).'/ip'))
  28.             return 0;
  29.         else
  30.             file_put_contents(dirname(__FILE__).'/ip', $ip);

  31.     AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)
  32.         ->regionId('cn-hangzhou')// replace regionId as you need
  33.         ->asDefaultClient();

  34.     $request = AlibabaCloud::rpc()
  35.         ->product('Alidns')
  36.         ->scheme('http')
  37.         ->version('2015-01-09')
  38.         ->action('DescribeSubDomainRecords')
  39.         ->method('POST')
  40.         ->host('alidns.aliyuncs.com');
  41.     foreach ($domains as $domain) {
  42.         $requestClone = clone $request;
  43.         $result = $requestClone->options([
  44.             'query' => [
  45.                 'RegionId'  => "default",
  46.                 'SubDomain' => $domain,
  47.             ],
  48.         ])->request();
  49.         $domainInfo = $result->toArray();
  50.         if (empty($domainInfo['DomainRecords']['Record'])) {
  51.             return;
  52.         }
  53.         $records = $domainInfo['DomainRecords']['Record'];
  54.         foreach ($records as $record) {
  55.             if ($record['Type'] == 'A') {
  56.                 $recordId = $record['RecordId'];
  57.                 $domainArr = explode('.', $domain);
  58.                 $res = AlibabaCloud::rpc()
  59.                     ->product('Alidns')
  60.                     ->scheme('http')
  61.                     ->version('2015-01-09')
  62.                     ->action('UpdateDomainRecord')
  63.                     ->method('POST')
  64.                     ->host('alidns.aliyuncs.com')
  65.                     ->options([
  66.                         'query' => [
  67.                             'RegionId' => "default",
  68.                             'RecordId' => $recordId,
  69.                             'RR'       => $domainArr[0],
  70.                             'Type'     => "A",
  71.                             'Value'    => $ip,
  72.                         ],
  73.                     ])
  74.                     ->request();
  75.                 file_put_contents(dirname(__FILE__).'/log.txt', json_encode($res) . PHP_EOL, FILE_APPEND);
  76.             }
  77.         }
  78.     }
  79. } catch (ClientException $e) {
  80.     echo $e->getErrorMessage() . PHP_EOL;
  81.     file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
  82. } catch (ServerException $e) {
  83.     echo $e->getErrorMessage() . PHP_EOL;
  84.     file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
  85. } catch (\Throwable $e){
  86.     echo $e->getErrorMessage() . PHP_EOL;
  87.     file_put_contents(dirname(__FILE__).'/log.txt', $e->getErrorMessage() . PHP_EOL, FILE_APPEND);
  88. }
复制代码


4.设置计划任务 每小时执行一次
  1. php /www/ddns/ddns.php
复制代码





欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://www.xmspace.net/) Powered by Discuz! X3.4