[点晴永久免费OA]PHP更新本地公网IP到阿里云域名解析,实现DDNS
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
<?php #需要配置的项 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 /*关键如何获取解析记录ID ,需要在下面这个地址中 https://api.aliyun.com/?spm=a2c4g.11186623.2.26.79ae2846d9RBBa#/?product=Alidns&api=AddDomainRecord¶ms={}&tab=DEMO&lang=JAVA 先设置一个记录(二级域名) 然后返回结果中获取这个ID 添加记录所需的参数文档在下面这里查看 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 排序参数计算签名($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 获取当前的解析记录值() { #请求dns的API重新做解析 $api_url = ''https://alidns.aliyuncs.com/?''; $data = []; #公共参数 $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"); #专用参数 $data[''Action''] = ''DescribeDomainRecordInfo''; $data[''RecordId''] = RECORDID; #解析记录的ID,此参数在添加解析时会返回,在获取域名解析列表时会返回 #签名 $data[''Signature''] = 排序参数计算签名($data); #请求接口 $json = 获取HTTP($api_url . http_build_query($data)); $result = json_decode($json,true); return $result[''Value'']; } function 主函数() { #获取现在网络的IP $ip = 获取HTTP(GET_IP_URL); #获取现在域名的解析结果 也就是旧的IP $旧IP = 获取当前的解析记录值(); #对比新旧IP是否一样 if(!isset($旧IP) || $旧IP !== $ip) { #请求dns的API重新做解析 $api_url = ''https://alidns.aliyuncs.com/?''; $data = []; #公共参数 $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"); #专用参数 $data[''Action''] = ''UpdateDomainRecord''; $data[''RecordId''] = RECORDID; #解析记录的ID,此参数在添加解析时会返回,在获取域名解析列表时会返回 $data[''RR''] = RR; #主机记录 $data[''Type''] = ''A''; $data[''Value''] = $ip; $data[''TTL''] = 600; #$data[''Priority''] = 10; #MX记录的优先级,取值范围[1,10],记录类型为MX记录时,此参数必须 $data[''Line''] = ''default''; #签名 $data[''Signature''] = 排序参数计算签名($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没有发生变化,不需要修改解析记录''; } } 主函数();
该文章在 2019/9/5 23:27:09 编辑过 |
关键字查询
相关文章
正在查询... |