list.txt每行一条
格式
xxx.com:ip.ip.ip.ip
- <?php
- $sub_domains = array(‘www’, ‘m’, ‘image’);
- $mail = ‘[email protected]’;
- $key = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
- $lines = file(‘list.txt’);
- foreach ($lines as $line) {
- $line = trim($line);
- if(empty($line)){
- continue;
- }
- list($domain, $ip) = explode(‘:’, $line);
- $domain = trim($domain);
- $ip = trim($ip);
- if(empty($domain) || empty($ip)){
- continue;
- }
- $id = add_domain($domain);
- if(empty($id)){
- echo "[$domain] add domain error!\n";
- exit();
- }else{
- echo "[$domain] domain added($id)\n";
- }
- foreach ($sub_domains as $sub_domain) {
- $domain_name = "{$sub_domain}.{$domain}";
- $res = add_dns($id, $domain_name, $ip);
- if(empty($res)){
- echo "[$domain_name] add dns record error\n";
- exit();
- }else{
- echo "[$domain_name] dns record added\n";
- }
- }
- echo "\n";
- }
- function add_dns($id, $name, $ip){
- global $mail, $key;
- $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",’Content-Type: application/json’);
- $data = array(‘type’=>’A’, ‘name’=>$name, ‘content’=>$ip, ‘ttl’=>120, ‘priority’=>10, ‘proxied’=>false);
- $res = curl_post("https://api.cloudflare.com/client/v4/zones/{$id}/dns_records", $post_header, $data);
- $array = json_decode($res, true);
- return $array[‘success’];
- }
- function add_domain($domain){
- global $mail, $key;
- $post_header = array("X-Auth-Email: {$mail}", "X-Auth-Key: $key",’Content-Type: application/json’);
- $data = array(‘type’=>"full", ‘name’=>$domain);
- $res = curl_post(‘https://api.cloudflare.com/client/v4/zones’, $post_header, $data);
- $array = json_decode($res, true);
- if(empty($array[‘success’]) || empty($array[‘result’][‘id’])){
- return false;
- }else{
- return $array[‘result’][‘id’];
- }
- }
- function curl_post($post_url, $post_header, $post_data){
- $ch = curl_init ();
- curl_setopt($ch, CURLOPT_POST , 1);
- curl_setopt($ch, CURLOPT_HEADER , 0);
- curl_setopt($ch, CURLOPT_URL , $post_url);
- curl_setopt($ch, CURLOPT_USERAGENT, ‘mjj’);
- curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($post_data));
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT,600);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
复制代码
甘肃网友:感谢分享