利用 DNSPod 开放API 实现域名动态解析 PHP版

vim ddns.php

< ?php
$domain_id  = 3441736;
$sub_domain = 'blog';
$record_id  = 23626557;
function ddns_curl($api, $data=array()){
        $data['login_email'] = '登录邮箱';
        $data['login_password'] = '登录密码';
        $data['format'] = 'json';
        $data['lang'] = 'en';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://dnsapi.cn/'.$api);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_USERAGENT, 'DNSPod API PHP Web Client/0.1 (chinanou.com@gmail.com)');
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response);
}
function ddns_log($msg){
        echo date('Y-m-d H:i:s')," {\n",var_dump($msg),"}\n";
}
$res = ddns_curl('Record.List', array( 'domain_id' => $domain_id, 'sub_domain' => $sub_domain ));
if($res->status->code != 1){
        ddns_log($res);
}else{
        $record = $res->records[0];
        $currentIp = file_get_contents('http://t.nt00.com/ip.php');
        ddns_log($record->value.' > '.$currentIp);
        if($record->value != $currentIp){
                $res = ddns_curl('Record.Modify', array(
                        'domain_id' => $domain_id,
                        'record_id' => $record_id,
                        'sub_domain' => $sub_domain,
                        'record_type' => 'A',
                        'record_line' => '默认',
                        'value' => $currentIp,
                        'ttl' => 120
                ));
                ddns_log($res);
        }
}

#cronetab -e
*/2 * * * * /xxx/bin/php -f /xxx/ddns.php >> ddns.log

发表评论

电子邮件地址不会被公开。 必填项已用*标注