list.txt每行一条
格式
xxx.com:ip.ip.ip.ip

  1. <?php
  2. $sub_domains = array(‘www’, ‘m’, ‘image’);
  3. $mail = ‘[email protected]’;
  4. $key = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
  5. $lines = file(‘list.txt’);
  6. foreach ($lines as $line) {
  7.         $line = trim($line);
  8.         if(empty($line)){
  9.                 continue;
  10.         }
  11.         list($domain, $ip) = explode(‘:’, $line);
  12.         $domain = trim($domain);
  13.         $ip = trim($ip);
  14.         if(empty($domain) || empty($ip)){
  15.                 continue;
  16.         }
  17.         $id = add_domain($domain);
  18.         if(empty($id)){
  19.                 echo "[$domain] add domain error!\n";
  20.                 exit();
  21.         }else{
  22.                 echo "[$domain] domain added($id)\n";
  23.         }
  24.         foreach ($sub_domains as $sub_domain) {
  25.                 $domain_name = "{$sub_domain}.{$domain}";
  26.                 $res = add_dns($id, $domain_name, $ip);
  27.                 if(empty($res)){
  28.                         echo "[$domain_name] add dns record error\n";
  29.                         exit();
  30.                 }else{
  31.                         echo "[$domain_name] dns record added\n";
  32.                 }
  33.         }
  34.         echo "\n";
  35. }
  36. function add_dns($id, $name, $ip){
  37.         global $mail, $key;
  38.         $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",’Content-Type: application/json’);
  39.         $data = array(‘type’=>’A’, ‘name’=>$name, ‘content’=>$ip, ‘ttl’=>120, ‘priority’=>10, ‘proxied’=>false);
  40.         $res = curl_post("https://api.cloudflare.com/client/v4/zones/{$id}/dns_records", $post_header, $data);
  41.         $array = json_decode($res, true);
  42.         return $array[‘success’];
  43. }
  44. function add_domain($domain){
  45.         global $mail, $key;
  46.         $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",’Content-Type: application/json’);
  47.         $data = array(‘type’=>"full", ‘name’=>$domain);
  48.         $res = curl_post(‘https://api.cloudflare.com/client/v4/zones&#8217;, $post_header, $data);
  49.         $array = json_decode($res, true);
  50.         if(empty($array[‘success’]) || empty($array[‘result’][‘id’])){
  51.                 return false;
  52.         }else{
  53.                 return $array[‘result’][‘id’];
  54.         }
  55. }
  56. function curl_post($post_url, $post_header, $post_data){
  57.     $ch = curl_init ();
  58.     curl_setopt($ch, CURLOPT_POST , 1);
  59.     curl_setopt($ch, CURLOPT_HEADER , 0);
  60.     curl_setopt($ch, CURLOPT_URL , $post_url);
  61.     curl_setopt($ch, CURLOPT_USERAGENT, ‘mjj’);
  62.     curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($post_data));
  63.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  64.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  65.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  66.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  67.     curl_setopt($ch, CURLOPT_TIMEOUT,600);
  68.     curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
  69.     $result = curl_exec($ch);
  70.     curl_close($ch);
  71.     return $result;
  72. }

复制代码

甘肃网友:感谢分享

01-21 12:53