PHP(Cloudflare动态更新DNS)

2019年4月13日 0 条评论 94 次阅读 1 人点赞

用来动态提交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: [email protected]',
    '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)

你必须 登录 才能发表评论