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 主函數();

用定時任務執行腳本便可php

相關文章
相關標籤/搜索