define('AccessKeyId', 'AccessKeyId');
define('AccessKeySecret', 'AccessKeySecret');
date_default_timezone_set('UTC');
/**
* 获取请求签名
*/
function getSign($add)
{
$nonce = time() . rand(11111, 99999);
$time = date('Y-m-d') . 'T' . date('H:i:s') . 'Z';
$data = array(
'AccessKeyId' => AccessKeyId,
'Format' => 'json',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => $nonce,
'SignatureVersion' => '1.0',
'Timestamp' => $time,
'Version' => '2015-01-09',
);
$data = array_merge($data, $add);
ksort($data);
$format = http_build_query($data);
$signUrl = 'GET&%2F&' . urlencode($format);
$sign = urlencode(base64_encode(hash_hmac('sha1', $signUrl, AccessKeySecret . '&', true)));
return array('url' => $format, 'sign' => $sign);
}
/**
* 获取域名列表
*/
function getDomains()
{
$sign = getSign(['Action' => 'DescribeDomains']);
$url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
$data = json_decode(httpRequest($url));
if (isset($data->Domains)) {
return $data;
} else {
return false;
}
}
/**
* 获取域名解析记录
*/
function getDomainRecords($domain)
{
$sign = getSign(['Action' => 'DescribeDomainRecords', 'DomainName' => $domain]);
$url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
$data = json_decode(httpRequest($url));
if (isset($data->DomainRecords)) {
return $data;
} else {
return false;
}
}
/**
* 获取子域名解析记录
*/
function getSubDomainRecords($domain)
{
$sign = getSign(['Action' => 'DescribeSubDomainRecords', 'SubDomain' => $domain]);
$url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
$dns = json_decode(httpRequest($url));
if (isset($dns->DomainRecords)) {
return $dns;
} else {
return false;
}
}
/**
* 更新域名解析
*/
function updateSubDomainRecords($id, $name, $ip)
{
$data = array(
'Action' => 'UpdateDomainRecord',
'RecordId' => $id,
'RR' => $name,
'Type' => 'A',
'Value' => $ip,
);
$sign = getSign($data);
$url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
return httpRequest($url);
}
/**
* 发起 HTTP 请求
*/
function httpRequest($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
return $error;
} else {
return $data;
}
}