1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| <?php
define('ACCESSKEYID','(访问ID)'); define('ACCESSKEYSECRET','(访问密钥)'); define('RR','@'); define('DOMAIN','hnsac.com'); define('RECORDID','(记录ID)');
function GetHTTP($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 Encoding($string) { $result = urlencode($string); $result = str_replace(['+', '*'], ['%20', '%2A'], $result); $result = preg_replace('/%7E/', '~', $result); return $result; }
function CalcSign($parameters,$method="GET") { ksort($parameters); $canonicalizedQuery = ''; foreach ($parameters as $key => $value) { $canonicalizedQuery .= '&' . Encoding($key) . '=' . Encoding($value); }
$stringToBeSigned = $method. '&%2F&'. Encoding(substr($canonicalizedQuery, 1)); return base64_encode(hash_hmac('sha1', $stringToBeSigned, ACCESSKEYSECRET . '&', true)); }
function Main() { if($_SERVER['REQUEST_METHOD']=="POST"){ $request = $_POST; } if($_SERVER['REQUEST_METHOD']=="GET"){ $request = $_GET; }
$ip = $request['ip'];
$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; $data['RR'] = RR; $data['Type'] = 'A'; $data['Value'] = $ip; $data['TTL'] = 600;
$data['Line'] = 'default';
$data['Signature'] = CalcSign($data);
$json = GetHTTP($api_url . http_build_query($data)); $result = json_decode($json); } Main();
|