欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cloudfare(简称:cf)批量添加域名并解析域名php脚本代码

程序员文章站 2022-03-11 14:20:55
...

cloudfare是全球最大的cdn免费提供服务商,很多seo业内的人员都用过也了解的cdn服务商, 经常需要用cloudfare CDN解析域名的SEO人员、站长们可以试试下面发布的cloudfare批量添加并解析域名的php脚本案例。

  1. <?php
  2. function post_data($url, $post=null, $header=array(), $timeout=8, $https=0)
  3. {
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  7. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  8. curl_setopt($ch, CURLOPT_HEADER, 0);
  9. if ($https) // https
  10. {
  11. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  12. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  13. }
  14. if ($header)
  15. {
  16. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  17. }
  18. if ($post)
  19. {
  20. curl_setopt($ch, CURLOPT_POST, 1);
  21. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
  22. }
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24. $content = curl_exec($ch);
  25. curl_close($ch);
  26. return $content;
  27. }
  28. // header 共用
  29. $header = array(
  30. "X-Auth-Email:164**4@qq.com",
  31. "X-Auth-Key:3957b2cba1bb**f979e4bc3bbe8b51d7",
  32. "Content-Type:application/json"
  33. );
  34. $domain = file_get_contents('./domain.txt');
  35. $domain = explode("\r\n", $domain);
  36. $record = file_get_contents('./record.txt');
  37. $record = explode("\r\n", $record);
  38. foreach ($domain as $v_domain)
  39. {
  40. // 添加域名
  41. $url = "https://api.cloudflare.com/client/v4/zones";
  42. $post = array(
  43. "name" => $v_domain,
  44. "jump_start" => true
  45. );
  46. $post = json_encode($post);
  47. $rs = post_data($url, $post, $header, 8, 1);
  48. $rs = json_decode($rs, true);
  49. if ($rs['success'] == false)
  50. {
  51. echo '添加失败,错误原因:' . $rs['errors'][0]['message'] . "\n";
  52. continue;
  53. }
  54. else
  55. {
  56. echo '添加域名成功' . "\n";
  57. echo '域名id:' . $rs['result']['id'] . "\n";
  58. echo '域名:' . $rs['result']['name'] . "\n";
  59. echo '域名状态:' . $rs['result']['status'] . "\n";
  60. echo '开始添加解析' . "\n";
  61. $zoneid = $rs['result']['id'];
  62. }
  63. foreach ($record as $v_record)
  64. {
  65. // 添加解析
  66. $url_add_records = "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records";
  67. $record_detail = explode(',', $v_record);
  68. $name = strtolower($record_detail[0]);
  69. $type = strtoupper($record_detail[1]);
  70. $ip = $record_detail[2];
  71. $post = array(
  72. "type" => $type,
  73. "name" => $name,
  74. "content" => $ip,
  75. "ttl" => 120, // 1 为自动
  76. "priority" => 10,
  77. "proxied" => true // true 为开启 dns and http proxy (cdn)
  78. );
  79. $post = json_encode($post);
  80. $add_records_rs = post_data($url_add_records, $post, $header, 8, 1);
  81. $rs = json_decode($add_records_rs, true);
  82. if ($rs['success'] == false)
  83. {
  84. echo '记录添加失败,错误原因:' . $rs['errors'][0]['message'] . "\n";
  85. }
  86. else
  87. {
  88. echo '记录添加成功' . "\n";
  89. }
  90. }
  91. }
  1. X-Auth-Email:添加你的cloudfare邮箱
  2. X-Auth-Key:你的cloudfareapi key

代码使用说明:domain.txt添加你的根域名一行一条
record.txt文本里添加解析ip 解析你的域名ip格式为:@,A,111.120.11.120 *,A,111.120.11.120 www,A,111.120.11.120 (分三行即可解析根域名 www域名 泛域名)一行一条 别搞错