阿里云 云解析


  1 <?php
  2 namespace App\Org;
  3 define('AccessKeyId', config('app.AliyunAccessKeyId'));
  4 define('AccessKeySecret', config('app.AliyunAccessKeySecret'));
  5 date_default_timezone_set('UTC');
  6 
  7 class Dns
  8 {
  9     const DomainName='aaa.com';
 10     const IP='**.**.**.**';
 11     /**
 12      *  生成请求签名
 13      */
 14     public static function getSign($add)
 15     {
 16         $nonce = time() . rand(11111, 99999);
 17         $time = date('Y-m-d') . 'T' . date('H:i:s') . 'Z';
 18         $data = array(
 19             'AccessKeyId' => AccessKeyId,
 20             'Format' => 'json',
 21             'SignatureMethod' => 'HMAC-SHA1',
 22             'SignatureNonce' => $nonce,
 23             'SignatureVersion' => '1.0',
 24             'Timestamp' => $time,
 25             'Version' => '2015-01-09',
 26             'Lang' => 'cn',
 27         );
 28         $data = array_merge($data, $add);
 29         ksort($data);
 30         $format = http_build_query($data);
 31         $signUrl = 'GET&%2F&' . urlencode($format);
 32         $sign = urlencode(base64_encode(hash_hmac('sha1', $signUrl, AccessKeySecret . '&', true)));
 33         return array('url' => $format, 'sign' => $sign);
 34     }
 35 
 36     //根据id获取记录信息
 37     public static function DescribeDomainRecordInfo($id)
 38     {
 39         $sign_add=[
 40             'Action' => 'DescribeDomainRecordInfo',
 41             'RecordId' => $id,
 42         ];
 43         $sign = self::getSign($sign_add);
 44         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
 45         $res = json_decode(self::httpRequest($url));
 46         if (!isset($res->RecordId)) {
 47             throw new \Exception('记录不存在',-1);
 48         }
 49         return $res;
 50     }
 51 
 52 
 53     /**
 54      *  获取域名解析记录
 55      */
 56     public static function getDomainRecords($add)
 57     {
 58         $sign_add=[
 59             'Action' => 'DescribeDomainRecords',
 60             'DomainName' => self::DomainName,
 61         ];
 62         $sign_add=array_merge($sign_add,$add);
 63         $sign = self::getSign($sign_add);
 64         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
 65         $data = json_decode(self::httpRequest($url));
 66         if (isset($data->Code)) {
 67             throw new \Exception($data->Message,-1);
 68         }
 69         $data_res=[
 70             'list'=>$data->DomainRecords->Record,
 71             'count'=>$data->TotalCount,
 72         ];
 73         return $data_res;
 74     }
 75 
 76     /**
 77      *  更新域名解析记录
 78      */
 79     public static function UpdateDomainRecord($add)
 80     {
 81         $sign_add=[
 82             'Action' => 'UpdateDomainRecord',
 83             'DomainName' => self::DomainName,
 84             'Type' => 'A',
 85             'Value' => self::IP,
 86         ];
 87         $sign_add=array_merge($sign_add,$add);
 88         $sign = self::getSign($sign_add);
 89         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
 90         $res= json_decode(self::httpRequest($url));
 91         if(isset($res->Code))
 92         {
 93             throw new \Exception($res->Message,-1);
 94         }
 95         return $res;
 96     }
 97 
 98     /**
 99      *  添加域名解析记录
100      */
101     public static function addDomainRecords($add)
102     {
103         $sign_add=[
104             'Action' => 'AddDomainRecord',
105             'DomainName' => self::DomainName,
106             'Type' => 'A',
107             'Value' => self::IP,
108         ];
109         $sign_add=array_merge($sign_add,$add);
110         $sign = self::getSign($sign_add);
111         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
112         $res= json_decode(self::httpRequest($url));
113         if(isset($res->Code))
114         {
115             throw new \Exception($res->Message,-1);
116         }
117         return $res;
118     }
119 
120     /**
121      *  更新域名解析记录
122      */
123     public static function deleteDomainRecord($add)
124     {
125         $sign_add=[
126             'Action' => 'DeleteDomainRecord',
127         ];
128         $sign_add=array_merge($sign_add,$add);
129         $sign = self::getSign($sign_add);
130         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
131         $res= json_decode(self::httpRequest($url));
132         if(isset($res->Code))
133         {
134             throw new \Exception($res->Message,-1);
135         }
136         return $res;
137     }
138 
139     public static function UpdateDomainRecordRemark($add)
140     {
141         $sign_add=[
142             'Action' => 'UpdateDomainRecordRemark',
143         ];
144         $sign_add=array_merge($sign_add,$add);
145         $sign = self::getSign($sign_add);
146         $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
147         $res= json_decode(self::httpRequest($url));
148         if(!isset($res->RequestId))
149         {
150             throw new \Exception($res->Message,-1);
151         }
152         return $res;
153     }
154 
155     /**
156      *  发起 HTTP 请求
157      */
158     public static function httpRequest($url)
159     {
160         $curl = curl_init();
161         curl_setopt($curl, CURLOPT_URL, $url);
162         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
163         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
164         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
165         $data = curl_exec($curl);
166         $error = curl_error($curl);
167         curl_close($curl);
168         if ($error) {
169             return $error;
170         } else {
171             return $data;
172         }
173     }
174 
175 }