cloudflare 利用API将域名批量解析到cloudflare

之前做站群有这个需求,所以在这里分享和记录下来
<?php
sleep(10);
$num = $_GET['num'];

if($num == ""){
    $num = 0;
}

echo "<h1>批量域名添加到cloudflare脚本</h1>";

//这里是子域名
$sub_domains = array('www', '@','static', 'm','v','news');
//$sub_domains = array('@');

$mail = '这里填写你的账户';
$key = '这里填写秘钥';

$lines = file('list.txt');

if($num < sizeof($lines)){
//重装数组
$lines = array($lines[$num]);

}else{
    
    echo "程序结束";
    exit();
}
foreach ($lines as $line) {
    
        $line = trim($line);
        if(empty($line)){
                continue;
        }

        list($domain, $ip) = explode(':', $line);
        $domain = trim($domain);
        $ip = trim($ip);

        if(empty($domain) || empty($ip)){
                continue;
        }

        $id = add_domain($domain);
        //$id ="1";

        if(empty($id)){
                echo "[$domain] add domain error!<br>";
                
                 $file_err = fopen("domains_error.txt",'a');
                 fwrite($file_err,$domain."\n");
                 fclose($file_err);
                
                exit();
        }else{
                echo "[$domain] domain added($id)<br>";
        }
                $ip_ = $ip;
        foreach ($sub_domains as $sub_domain) {
                
                $type = "CNAME";
                //$domain_name = "{$sub_domain}.{$domain}";
                $domain_name = $sub_domain;
                if($sub_domain == '@'){
                
                    //$domain_name = $domain;
                    $type="A";
                    $ip = $ip_;
                    
                }else{
                    $ip=$domain;
                    //$domain_name=$sub_domain;
                }
                
            //    echo $type.":".$domain_name." => $ip<br>";
               //continue;
            
                $res = add_dns($id, $domain_name, $ip,$type);
                
                if(empty($res)){
                        echo "[$domain_name] add dns record error<br>";
                        
                        $file_err = fopen("domains_error.txt",'a');
                        fwrite($file_err,$domain_name.$domain."\n");
                        fclose($file_err);
                        
                        exit();
                }else{
                        echo "[$domain_name] dns record added<br>";
                }
                
        }
        echo "<br>=============================================================================<br>";

}
$num++;
echo "<script>window.location.href='填写脚本地址?num=$num';</script>";

function add_dns($id, $name, $ip,$type){
        global $mail, $key;
        $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",'Content-Type: application/json');
        $data = array('type'=>$type, 'name'=>$name, 'content'=>$ip, 'ttl'=>1, 'priority'=>10, 'proxied'=>true);

        $res = curl_post("https://api.cloudflare.com/client/v4/zones/{$id}/dns_records", $post_header, $data);
        $array = json_decode($res, true);

        return $array['success'];
}

function add_domain($domain){
        global $mail, $key;
        $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",'Content-Type: application/json');
        $data = array('type'=>"full", 'name'=>$domain);
        $res = curl_post('https://api.cloudflare.com/client/v4/zones', $post_header, $data);
        $array = json_decode($res, true);

        if(empty($array['success']) || empty($array['result']['id'])){
                return false;
        }else{
                return $array['result']['id'];
        }
}

function curl_post($post_url, $post_header, $post_data){
    $ch = curl_init ();
    curl_setopt($ch, CURLOPT_POST , 1);
    curl_setopt($ch, CURLOPT_HEADER , 0);
    curl_setopt($ch, CURLOPT_URL , $post_url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'mjj');
    curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($post_data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT,3600);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页