PHP(Cloudflare动态更新DNS)

1,257 次浏览次阅读

用来动态提交 Cloudflare 域名 DNS

function getAndPost($url,$header='',$isPut=false,$data=''){$ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    if (is_array($header)){curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    }
    if ($isPut){curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$ip=getAndPost("https://www.topupgo.com/ip.php");
$hostID='';
$hostUrlID='';
$url='https://api.cloudflare.com/client/v4/zones/'.$hostUrlID.'/dns_records/'.$hostID;
$api_key='';
$header=array(
    'X-Auth-Email: account@email.com',
    'X-Auth-Key:'.$api_key,
    'Content-Type: application/json'
);
$result= getAndPost($url,$header);
$result=json_decode($result,true);
if (!isset($result['result']['content'])){
    $nowIP='';
    var_dump($result);
    exit;
}else{$nowIP=$result['result']['content'];
}
if ($ip!=$nowIP && $nowIP!=''&& $ip!='' && count(explode('.',$ip))==4){
        $data=array('type'=>'A',
            'name'=>'host.your.com',
            'content'=>$ip,
            'proxied'=>false //cdn
        );
        $ip=getAndPost($url,$header,true,json_encode($data));
    }
echo $ip;

正文完
 0
yxsoft
版权声明:本站原创文章,由 yxsoft 于2019-04-13发表,共计4004字。
转载说明:除特殊说明外本站文章皆由YXSoft发布,转载请注明出处。