[點(diǎn)晴永久免費(fèi)OA]PHP更新本地公網(wǎng)IP到阿里云域名解析,實(shí)現(xiàn)DDNS
<?php #需要配置的項(xiàng) define(''ACCESSKEYID'',''AAAAAAAAAAAAAAAA''); #阿里云用戶密鑰ID 獲取方法 https://help.aliyun.com/knowledge_detail/38738.html define(''ACCESSKEYSECRET'',''AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'');#阿里云用戶密鑰 define(''RR'',''myhome''); #二級域名的記錄名 如果是一級 請用@ define(''DOMAIN'',''myhome.baidu.com''); #完整域名 define(''RECORDID'',''11111111111111111''); #解析記錄ID /*關(guān)鍵如何獲取解析記錄ID ,需要在下面這個(gè)地址中 https://api.aliyun.com/?spm=a2c4g.11186623.2.26.79ae2846d9RBBa#/?product=Alidns&api=AddDomainRecord¶ms={}&tab=DEMO&lang=JAVA 先設(shè)置一個(gè)記錄(二級域名) 然后返回結(jié)果中獲取這個(gè)ID 添加記錄所需的參數(shù)文檔在下面這里查看 https://help.aliyun.com/document_detail/29772.html?spm=a2c4g.11186623.4.3.392b5eb4SAeph9 */ define(''GET_IP_URL'',''myip.fireflysoft.net''); define(''IP_FILE'',__DIR__. DIRECTORY_SEPARATOR .''ip.txt''); function 獲取HTTP($url) { $curl_samples = curl_init(); $options = [CURLOPT_URL => $url,CURLOPT_RETURNTRANSFER => 1,CURLOPT_FOLLOWLOCATION => 1,CURLOPT_SSL_VERIFYPEER => 0]; curl_setopt_array($curl_samples, $options); curl_setopt($curl_samples, CURLOPT_CUSTOMREQUEST, ''GET''); $result = curl_exec($curl_samples); $info = curl_getinfo($curl_samples); $curl_err = curl_errno($curl_samples); curl_close($curl_samples); if( 0 != $curl_err ) { echo "getip error:".date(''Y-m-d H:i:s'',time()); var_dump($info); echo PHP_EOL; var_dump($result); echo PHP_EOL; echo $curl_err; echo PHP_EOL; exit; } return $result; } function 編碼字符($string) { $result = urlencode($string); $result = str_replace([''+'', ''*''], [''%20'', ''%2A''], $result); $result = preg_replace(''/%7E/'', ''~'', $result); return $result; } function 排序參數(shù)計(jì)算簽名($parameters,$method="GET") { ksort($parameters); $canonicalizedQuery = ''''; foreach ($parameters as $key => $value) { $canonicalizedQuery .= ''&'' . 編碼字符($key) . ''='' . 編碼字符($value); } $stringToBeSigned = $method. ''&%2F&''. 編碼字符(substr($canonicalizedQuery, 1)); return base64_encode(hash_hmac(''sha1'', $stringToBeSigned, ACCESSKEYSECRET . ''&'', true)); } function 獲取當(dāng)前的解析記錄值() { #請求dns的API重新做解析 $api_url = ''https://alidns.aliyuncs.com/?''; $data = []; #公共參數(shù) $data[''Format''] = ''json''; $data[''Version''] = ''2015-01-09''; $data[''SignatureMethod''] = ''HMAC-SHA1''; $data[''SignatureNonce''] = uniqid(); $data[''SignatureVersion'']= ''1.0''; $data[''AccessKeyId''] = ACCESSKEYID; $data[''Timestamp''] = gmdate("Y-m-d\TH:i:s\Z"); #專用參數(shù) $data[''Action''] = ''DescribeDomainRecordInfo''; $data[''RecordId''] = RECORDID; #解析記錄的ID,此參數(shù)在添加解析時(shí)會返回,在獲取域名解析列表時(shí)會返回 #簽名 $data[''Signature''] = 排序參數(shù)計(jì)算簽名($data); #請求接口 $json = 獲取HTTP($api_url . http_build_query($data)); $result = json_decode($json,true); return $result[''Value'']; } function 主函數(shù)() { #獲取現(xiàn)在網(wǎng)絡(luò)的IP $ip = 獲取HTTP(GET_IP_URL); #獲取現(xiàn)在域名的解析結(jié)果 也就是舊的IP $舊IP = 獲取當(dāng)前的解析記錄值(); #對比新舊IP是否一樣 if(!isset($舊IP) || $舊IP !== $ip) { #請求dns的API重新做解析 $api_url = ''https://alidns.aliyuncs.com/?''; $data = []; #公共參數(shù) $data[''Format''] = ''json''; $data[''Version''] = ''2015-01-09''; $data[''SignatureMethod''] = ''HMAC-SHA1''; $data[''SignatureNonce''] = uniqid(); $data[''SignatureVersion'']= ''1.0''; $data[''AccessKeyId''] = ACCESSKEYID; $data[''Timestamp''] = gmdate("Y-m-d\TH:i:s\Z"); #專用參數(shù) $data[''Action''] = ''UpdateDomainRecord''; $data[''RecordId''] = RECORDID; #解析記錄的ID,此參數(shù)在添加解析時(shí)會返回,在獲取域名解析列表時(shí)會返回 $data[''RR''] = RR; #主機(jī)記錄 $data[''Type''] = ''A''; $data[''Value''] = $ip; $data[''TTL''] = 600; #$data[''Priority''] = 10; #MX記錄的優(yōu)先級,取值范圍[1,10],記錄類型為MX記錄時(shí),此參數(shù)必須 $data[''Line''] = ''default''; #簽名 $data[''Signature''] = 排序參數(shù)計(jì)算簽名($data); #請求接口 $json = 獲取HTTP($api_url . http_build_query($data)); $result = json_decode($json); #完成以后 if(isset($result->RecordId)){ file_put_contents(IP_FILE,$ip); }else{ echo ''error''; var_dump($result); } }else { echo ''IP沒有發(fā)生變化,不需要修改解析記錄''; } } 主函數(shù)();
該文章在 2019/9/5 23:27:09 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |