PHP更新本地公网IP到阿里云域名解析,实现DDNS

  1 <?php
  2 #需要配置的项
  3 define('ACCESSKEYID','AAAAAAAAAAAAAAAA');                  #阿里云用户密钥ID 获取方法 https://help.aliyun.com/knowledge_detail/38738.html
  4 define('ACCESSKEYSECRET','AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');#阿里云用户密钥
  5 define('RR','myhome');                                     #二级域名的记录名 如果是一级 请用@
  6 define('DOMAIN','myhome.baidu.com');                       #完整域名  
  7 define('RECORDID','11111111111111111');                    #解析记录ID
  8 /*关键如何获取解析记录ID ,需要在下面这个地址中
  9 https://api.aliyun.com/?spm=a2c4g.11186623.2.26.79ae2846d9RBBa#/?product=Alidns&api=AddDomainRecord&params={}&tab=DEMO&lang=JAVA
 10 先设置一个记录(二级域名) 然后返回结果中获取这个ID 添加记录所需的参数文档在下面这里查看
 11 https://help.aliyun.com/document_detail/29772.html?spm=a2c4g.11186623.4.3.392b5eb4SAeph9
 12 
 13 */
 14 
 15 define('GET_IP_URL','myip.fireflysoft.net');
 16 define('IP_FILE',__DIR__. DIRECTORY_SEPARATOR .'ip.txt');
 17 
 18 function 获取HTTP($url)
 19 {
 20     $curl_samples = curl_init();
 21     $options = [CURLOPT_URL => $url,CURLOPT_RETURNTRANSFER => 1,CURLOPT_FOLLOWLOCATION => 1,CURLOPT_SSL_VERIFYPEER => 0];
 22     curl_setopt_array($curl_samples, $options);                
 23     curl_setopt($curl_samples, CURLOPT_CUSTOMREQUEST, 'GET');  
 24     $result = curl_exec($curl_samples);                            
 25     $info = curl_getinfo($curl_samples); 
 26     $curl_err = curl_errno($curl_samples);
 27     curl_close($curl_samples);                                 
 28     if( 0 != $curl_err )
 29     {
 30         echo "getip error:".date('Y-m-d H:i:s',time());
 31         var_dump($info);
 32         echo PHP_EOL;
 33         var_dump($result);
 34         echo PHP_EOL;
 35         echo $curl_err;
 36         echo PHP_EOL;
 37         exit;
 38     }
 39     return $result;
 40 }
 41  
 42 function 编码字符($string)
 43 {
 44     $result = urlencode($string);
 45     $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
 46     $result = preg_replace('/%7E/', '~', $result);
 47     return $result;
 48 }
 49 
 50 function 排序参数计算签名($parameters,$method="GET")
 51 {
 52     ksort($parameters);
 53     $canonicalizedQuery = '';
 54     foreach ($parameters as $key => $value) 
 55     {
 56         $canonicalizedQuery .= '&' . 编码字符($key) . '=' . 编码字符($value);
 57     }
 58     
 59     $stringToBeSigned = $method. '&%2F&'. 编码字符(substr($canonicalizedQuery, 1));
 60     return base64_encode(hash_hmac('sha1', $stringToBeSigned, ACCESSKEYSECRET . '&', true));
 61 }
 62 
 63 function 获取当前的解析记录值()
 64 {
 65     #请求dns的API重新做解析
 66     $api_url = 'https://alidns.aliyuncs.com/?';
 67     $data = [];
 68     #公共参数
 69     $data['Format']          = 'json';
 70     $data['Version']         = '2015-01-09';
 71     $data['SignatureMethod'] = 'HMAC-SHA1';
 72     $data['SignatureNonce']  = uniqid();
 73     $data['SignatureVersion']= '1.0';
 74     $data['AccessKeyId']     = ACCESSKEYID; 
 75     $data['Timestamp']       = gmdate("Y-m-d\TH:i:s\Z");
 76     #专用参数
 77     $data['Action']     = 'DescribeDomainRecordInfo';
 78     $data['RecordId']   = RECORDID; #解析记录的ID,此参数在添加解析时会返回,在获取域名解析列表时会返回
 79     #签名
 80     $data['Signature']  = 排序参数计算签名($data);
 81     #请求接口
 82     $json = 获取HTTP($api_url . http_build_query($data));
 83     $result = json_decode($json,true);
 84     return $result['Value'];
 85     
 86 }
 87 
 88 function 主函数()
 89 {   
 90     #获取现在网络的IP
 91     $ip = 获取HTTP(GET_IP_URL);
 92     #获取现在域名的解析结果 也就是旧的IP
 93     $旧IP = 获取当前的解析记录值();
 94     #对比新旧IP是否一样
 95     if(!isset($旧IP) || $旧IP !== $ip)
 96     {
 97         #请求dns的API重新做解析
 98         $api_url = 'https://alidns.aliyuncs.com/?';
 99         $data = [];
100         #公共参数
101         $data['Format']          = 'json';
102         $data['Version']         = '2015-01-09';
103         $data['SignatureMethod'] = 'HMAC-SHA1';
104         $data['SignatureNonce']  = uniqid();
105         $data['SignatureVersion']= '1.0';
106         $data['AccessKeyId']     = ACCESSKEYID; 
107         $data['Timestamp']       = gmdate("Y-m-d\TH:i:s\Z");
108         #专用参数
109         $data['Action']     = 'UpdateDomainRecord';
110         $data['RecordId']   = RECORDID; #解析记录的ID,此参数在添加解析时会返回,在获取域名解析列表时会返回
111         $data['RR']         = RR; #主机记录
112         $data['Type']       = 'A';
113         $data['Value']      = $ip;
114         $data['TTL']        = 600;
115         #$data['Priority']   = 10; #MX记录的优先级,取值范围[1,10],记录类型为MX记录时,此参数必须
116         $data['Line']       = 'default';
117         #签名
118         $data['Signature']  = 排序参数计算签名($data);
119         #请求接口
120         $json = 获取HTTP($api_url . http_build_query($data));
121         $result = json_decode($json);
122         
123         #完成以后
124         if(isset($result->RecordId)){
125             file_put_contents(IP_FILE,$ip);
126         }else{
127             echo 'error';
128             var_dump($result);
129         }
130     }else
131     {
132         echo 'IP没有发生变化,不需要修改解析记录';
133     }
134 }
135 主函数();

用定时任务执行脚本即可

posted @ 2019-05-09 16:58  鱼肉真好吃  阅读(1970)  评论(0编辑  收藏  举报