2 Star 0 Fork 0

維克多 / ddns

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未指定开源许可证,未经作者的许可,此代码仅用于学习,不能用于其他用途。
克隆/下载
ddns_namesilo.php 3.50 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
Bruskyii Panda 提交于 2018-12-18 08:55 . Update ddns_namesilo.php
<?php
/**
* Namesilo DDNS
* SSRPanel 2018/12/15
* Usage:Add `crontab` Command:`* * * * * php /www/wwwroot/ddns_namesilo.php`
*/
class namesilo
{
private $apiKey;
private $domain;
private $rrhost;
function __construct()
{
$this->apiKey = 'c8082318267c813f91b36111'; // set your namesilo api key
$this->domain = 'ssrpanel.com'; // set your domain
$this->rrhost = 'hk'; // set your sub domain
}
function ddns()
{
$ip = $this->checkIP();
if (false === $ip) {
$this->log("IP NOT CHANGED");
exit();
}
$recordList = $this->dnsListRecords();
$rrid = '';
foreach ($recordList['reply']['resource_record'] as $vo) {
if ($vo['host'] == $this->rrhost . '.' . $this->domain) {
$rrid = $vo['record_id'];
break;
}
}
$this->log($rrid);
if ($rrid) {
$this->dnsUpdateRecord($rrid, $this->rrhost, $ip);
}
exit();
}
function dnsListRecords()
{
$query = [
'domain' => $this->domain
];
return $this->send('dnsListRecords', $query);
}
function dnsUpdateRecord($rrid, $rrhost, $rrvalue, $ttl = 7207)
{
$query = [
'domain' => $this->domain,
'rrid' => $rrid,
'rrhost' => $rrhost,
'rrvalue' => $rrvalue,
'rrttl' => $ttl
];
return $this->send('dnsUpdateRecord', $query);
}
function getIP()
{
$result = $this->curl('http://ip.taobao.com/service/getIpInfo.php?ip=myip');
$result = json_decode($result, true);
return $result['data']['ip'];
}
function checkIP()
{
$nowIP = $this->getIP();
$file = dirname(__FILE__) . '/ddns_namesilo.ip';
$oldIP = file_get_contents($file);
if ($nowIP == $oldIP) {
return false;
}
file_put_contents($file, $nowIP);
return $nowIP;
}
function send($operation, $data = [])
{
$params = [
'version' => 1,
'type' => 'xml',
'key' => $this->apiKey
];
$query = array_merge($params, $data);
$result = $this->curl('https://www.namesilo.com/api/' . $operation . '?' . http_build_query($query));
$result = $this->xmlToArray($result);
$this->log($result);
return $result;
}
function log($data)
{
$data = is_array($data) ? var_export($data, true) : $data;
$file = dirname(__FILE__) . '/ddns_namesilo.log';
file_put_contents($file, $data . "\r\n", FILE_APPEND);
}
function xmlToArray($xml)
{
libxml_disable_entity_loader(true);
$result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $result;
}
function curl($url, $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
if ($data) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
$namesilo = new namesilo();
$dnsListRecords = $namesilo->ddns();

评论 ( 0 )

你可以在登录后,发表评论

1
https://gitee.com/ggfvictor/ddns.git
git@gitee.com:ggfvictor/ddns.git
ggfvictor
ddns
ddns
master

搜索帮助