宝塔面板:IPV4/IPV6 动态域名解析,不到100行代码,一个定时任务,轻松搞定,自带企业微信通知
2024-11-07原创插件打野哥504°c
A+ A-前提条件:
1、西部数码域名一个,域名管理里直接复制32位 ApiKey,不要使用管理密码
2、宝塔面板定时任务一个


<?php
$api = 'https://api.west.cn/API/v2/';
$domain = 'rcku.cn'; // 你的域名
$hostname = 'home'; // 主机名
$apidomainkey = 'f4626c****e60c814a687****2e9ef83'; // 西部数码 域名 ApiKey 32位
$webhook_key = '8ac824d3-****-4b59-****-8434ae96c646'; // 企业微信机器人key,自行替换
$cache = __DIR__."/ddns.txt";
if(!$ipaddr){exit();}
if(!file_exists($cache)){file_put_contents($cache,1);}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://speed.neu6.edu.cn/getIP.php'); // 这是获取IPV6的
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$ipaddr = curl_exec($ch);
curl_close($ch);
if($ipaddr == file_get_contents($cache)){exit();}
file_put_contents($cache,$ipaddr);
$path = 'domain/dns/';
$data = array(
'act' => 'dnsrec.list',
'domain' => $domain,
'apidomainkey' => $apidomainkey,
);
$header = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$rs = request($api . $path, $header, http_build_query($data),"POST");
if(!isset($rs['body']['items'])){wx_robot("获取解析记录失败,请检查\n返回结果:\n" . json_encode($rs,256)); exit();}
$data = array(
'act' => 'dnsrec.modify',
'domain' => $domain,
'record_value' => $ipaddr,
'record_type' => 'AAAA', // AAAA为IPV6 A为IPV4
'record_ttl' => 60,
'hostname' => $hostname,
'apidomainkey' => $apidomainkey,
);
foreach ($rs['body']['items'] as $key => $v) {
if($v['hostname'] == $hostname){
$rs = request($api . $path, $header, http_build_query($data));
wx_robot("当前IP不一致\n旧:".$v['record_value']."\n新:". $ipaddr ."\n已发起更新,更新结果:\n" . json_encode($rs,256));
$check=1; break;
}
}
if(!isset($check)){
$data['act'] = 'dnsrec.add';
$rs = request($api . $path, $header, http_build_query($data));
wx_robot("解析不存在,添加解析\n解析结果:\n" . json_encode($rs,256));
}
function wx_robot($content)
{
global $webhook_key;
$mess = array(
'msgtype' => 'text',
'text' => array(
'content' => $content,
),
);
$api = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$webhook_key;
$result = request($api, ['Content-Type: application/json'],json_encode($mess), 'POST');
}
function request($Url, $Header = null, $Data = null){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Data);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,1);
}
未定义标签



