2021年2月

https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
先记下来
前期工作:
申请token,到这里:
https://dash.cloudflare.com/profile/api-tokens
创建对应域名的token。
权限需要两行,zone-edit,dns-edit。
cloudflare_dns_api.png

用PHP来实现的。因为windows下面不能用shell。

<?php
include "cloudflareAPI.php";

$ip_v4=file_get_contents("https://api-ipv4.ip.sb/ip");
$ip_v6=file_get_contents("https://api-ipv6.ip.sb/ip");
$cf=new CFAPI;
$cf->apikey="XXXXXXXXXXXXXXXXXXXXXXX"; //token
$cf->email="admin@kele.im";
$cf->zoneid="xxxxxxxxxxxxxxxx"; //域名的zoneid在域名overview页面右下角
echo $cf->updateDNS("ddns.domain.com",$ip_v4);
?>
<?php

class CFAPI {

    public $apikey;
    public $email;
    public $zoneid;

    private function curl($url,$method="GET",$data=null)
    {
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);                                                                     
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer '.$this->apikey.'',
        'Content-Type:application/json'
        ));
        if (!empty($data)) {
            $data_string = json_encode($data);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
        }
        $sonuc = curl_exec($ch);
        curl_close($ch);
        return $sonuc;
    }

    private function getDomainID($domain)
    {
        //$result=$this->curl("https://api.cloudflare.com/client/v4/zones/".$this->zoneid."/dns_records?name=".$domain);
        $result=$this->curl("https://api.cloudflare.com/client/v4/zones/".$this->zoneid."/dns_records?name=".$domain."&type=A");
        var_dump($result);
        $json=json_decode($result);
        if (!empty($json->result) and $json->success) {
            return $json->result[0]->id;
        }
        else
        {
            return "";
        }

    }

    public function getuser()
    {
        return $this->curl("https://api.cloudflare.com/client/v4/user");
    }

    public function updateDNS($domain,$ip,$type="A",$ttl=120)
    {
        $domainid=$this->getDomainID($domain);
        $data = array(
            'type' => ''.$type.'',
            'name' => ''.$domain.'',
            'content' => ''.$ip.'',
            'proxied' => false,
            'ttl' => $ttl
        );
        if (empty($domainid)) {
            return $this->curl("https://api.cloudflare.com/client/v4/zones/".$this->zoneid."/dns_records","POST",$data);
        }
        else
        {
            return $this->curl("https://api.cloudflare.com/client/v4/zones/".$this->zoneid."/dns_records/".$domainid,"PUT",$data);
        }
        
    }

}

?>

脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单。
#!/usr/bin/python3是告诉操作系统执行这个脚本的时候,调用 /usr/bin 下的 python3 解释器;

#!/usr/bin/env python3 这种用法是为了防止操作系统用户没有将 python3 装在默认的 /usr/bin 路径里。当系统看到这一行的时候,首先会到 env 设置里查找 python3 的安装路径,再调用对应路径下的解释器程序完成操作。

#!/usr/bin/python3 相当于写死了 python3 路径;

#!/usr/bin/env python3 会去环境设置寻找 python3 目录,推荐这种写法。

1,显示不可见字符
"draw_white_space": "all"
2,tab换成空格

// The number of spaces a tab is considered equal to
"tab_size": 4,

// Set to true to insert spaces when tab is pressed
"translate_tabs_to_spaces": false,

// If translate_tabs_to_spaces is true, use_tab_stops will make tab and
// backspace insert/delete up to the next tabstop
"use_tab_stops": true,