cloudfare(简称:cf)批量添加域名并解析域名php脚本代码
程序员文章站
2022-03-11 14:20:55
...
cloudfare是全球最大的cdn免费提供服务商,很多seo业内的人员都用过也了解的cdn服务商, 经常需要用cloudfare CDN解析域名的SEO人员、站长们可以试试下面发布的cloudfare批量添加并解析域名的php脚本案例。
<?php
function post_data($url, $post=null, $header=array(), $timeout=8, $https=0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 0);
if ($https) // https
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
}
if ($header)
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($post)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
// header 共用
$header = array(
"X-Auth-Email:164**4@qq.com",
"X-Auth-Key:3957b2cba1bb**f979e4bc3bbe8b51d7",
"Content-Type:application/json"
);
$domain = file_get_contents('./domain.txt');
$domain = explode("\r\n", $domain);
$record = file_get_contents('./record.txt');
$record = explode("\r\n", $record);
foreach ($domain as $v_domain)
{
// 添加域名
$url = "https://api.cloudflare.com/client/v4/zones";
$post = array(
"name" => $v_domain,
"jump_start" => true
);
$post = json_encode($post);
$rs = post_data($url, $post, $header, 8, 1);
$rs = json_decode($rs, true);
if ($rs['success'] == false)
{
echo '添加失败,错误原因:' . $rs['errors'][0]['message'] . "\n";
continue;
}
else
{
echo '添加域名成功' . "\n";
echo '域名id:' . $rs['result']['id'] . "\n";
echo '域名:' . $rs['result']['name'] . "\n";
echo '域名状态:' . $rs['result']['status'] . "\n";
echo '开始添加解析' . "\n";
$zoneid = $rs['result']['id'];
}
foreach ($record as $v_record)
{
// 添加解析
$url_add_records = "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records";
$record_detail = explode(',', $v_record);
$name = strtolower($record_detail[0]);
$type = strtoupper($record_detail[1]);
$ip = $record_detail[2];
$post = array(
"type" => $type,
"name" => $name,
"content" => $ip,
"ttl" => 120, // 1 为自动
"priority" => 10,
"proxied" => true // true 为开启 dns and http proxy (cdn)
);
$post = json_encode($post);
$add_records_rs = post_data($url_add_records, $post, $header, 8, 1);
$rs = json_decode($add_records_rs, true);
if ($rs['success'] == false)
{
echo '记录添加失败,错误原因:' . $rs['errors'][0]['message'] . "\n";
}
else
{
echo '记录添加成功' . "\n";
}
}
}
X-Auth-Email:添加你的cloudfare邮箱
X-Auth-Key:你的cloudfare的api key值
代码使用说明:domain.txt添加你的根域名一行一条
record.txt文本里添加解析ip 解析你的域名ip格式为:@,A,111.120.11.120 *,A,111.120.11.120 www,A,111.120.11.120 (分三行即可解析根域名 www域名 泛域名)一行一条 别搞错