<?php #需要配置的项 define('ACCESSKEYID',''); #阿里云用户密钥ID 获取方法 https://help.aliyun.com/knowledge_detail/38738.html define('ACCESSKEYSECRET','');#阿里云用户密钥 define('RR','myhome'); #二级域名的记录名 如果是一级 请用@ define('RECORDID',''); /*解析记录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','./ip.txt'); function 获取HTTP($url) { $curl_samples = curl_init(); $options = [CURLOPT_URL => $url,CURLOPT_RETURNTRANSFER => 1,CURLOPT_FOLLOWLOCATION => 1,]; curl_setopt_array($curl_samples, $options); curl_setopt($curl_samples, CURLOPT_CUSTOMREQUEST, 'GET'); $ip = 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($ip); exit; } return $ip; } 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 主函数() { $ip = 获取HTTP(GET_IP_URL); if(!file_exists(IP_FILE)) { file_put_contents(IP_FILE,$ip); }else { $旧IP = file_get_contents(IP_FILE); } 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'] = date('Y-m-d\TH:i:s\Z',time()); $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没有发生变化,不需要修改解析记录'; } } 主函数();
建立计划任务
执行 php.exe 脚本.php > log.txt