alydns.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. date_default_timezone_set("GMT");
  3. //这两个值需要去阿里云申请
  4. define("accessKeyId", "");
  5. define("accessSecrec", "");
  6. /*
  7. //$obj = new AliDns(accessKeyId, accessSecrec, "newyingyong.cn");
  8. //显示所有
  9. //$data = $obj->DescribeDomainRecords();
  10. //增加解析
  11. //$data= $obj->AddDomainRecord("TXT", "test", "test");
  12. //修改解析
  13. //$data = $obj->UpdateDomainRecord("3965724468724736","TXT", "test", "test2");
  14. //删除解析
  15. //$data = $obj->DescribeDomainRecords();
  16. //$data = $data["DomainRecords"]["Record"];
  17. //if (is_array($data)) {
  18. //foreach ($data as $v) {
  19. //if ($v["RR"] == "test") {
  20. //$obj->DeleteDomainRecord($v["RecordId"]);
  21. //}
  22. //}
  23. //}
  24. */
  25. /*
  26. example:
  27. php alydns.php "newyingyong.cn" "test" "test2"
  28. */
  29. ########## 配合 cerbot 运行
  30. echo $argv[1] . "-" . $argv[2] . "-" . $argv[3];
  31. $obj = new AliDns(accessKeyId, accessSecrec, $argv[1]);
  32. $data = $obj->DescribeDomainRecords();
  33. $data = $data["DomainRecords"]["Record"];
  34. if (is_array($data)) {
  35. foreach ($data as $v) {
  36. if ($v["RR"] == $argv[2]) {
  37. $obj->DeleteDomainRecord($v["RecordId"]);
  38. }
  39. }
  40. }
  41. print_r($obj->AddDomainRecord("TXT", $argv[2],$argv[3]));
  42. ############ Class 定义
  43. class AliDns {
  44. private $accessKeyId = null;
  45. private $accessSecrec = null;
  46. private $DomainName = null;
  47. public function __construct($accessKeyId, $accessSecrec, $domain) {
  48. $this->accessKeyId = $accessKeyId;
  49. $this->accessSecrec = $accessSecrec;
  50. $this->DomainName = $domain;
  51. }
  52. public function DescribeDomainRecords() {
  53. $requestParams = array(
  54. "Action" => "DescribeDomainRecords"
  55. );
  56. $val = $this->send($requestParams);
  57. return $this->out($val);
  58. }
  59. public function UpdateDomainRecord($id, $type, $rr,$value){
  60. $requestParams = array(
  61. "Action" => "UpdateDomainRecord",
  62. "RecordId" => $id,
  63. "RR" => $rr,
  64. "Type" => $type,
  65. "Value" => $value,
  66. );
  67. $val = $this->send($requestParams);
  68. return $this->out($val);
  69. }
  70. public function DeleteDomainRecord($id) {
  71. $requestParams = array(
  72. "Action" => "DeleteDomainRecord",
  73. "RecordId" => $id,
  74. );
  75. $val = $this->send($requestParams);
  76. return $this->out($val);
  77. }
  78. public function AddDomainRecord($type, $rr, $value) {
  79. $requestParams = array(
  80. "Action" => "AddDomainRecord",
  81. "RR" => $rr,
  82. "Type" => $type,
  83. "Value" => $value,
  84. );
  85. $val = $this->send($requestParams);
  86. return $this->out($val);
  87. }
  88. private function send($requestParams) {
  89. $publicParams = array(
  90. "DomainName" => $this->DomainName,
  91. "Format" => "JSON",
  92. "Version" => "2015-01-09",
  93. "AccessKeyId" => $this->accessKeyId,
  94. "Timestamp" => date("Y-m-d\TH:i:s\Z"),
  95. "SignatureMethod" => "HMAC-SHA1",
  96. "SignatureVersion" => "1.0",
  97. "SignatureNonce" => substr(md5(rand(1, 99999999)), rand(1, 9), 14),
  98. );
  99. $params = array_merge($publicParams, $requestParams);
  100. $params['Signature'] = $this->sign($params, $this->accessSecrec);
  101. $uri = http_build_query($params);
  102. $url = 'http://alidns.aliyuncs.com/?'.$uri;
  103. return $this->curl($url);
  104. }
  105. private function sign($params, $accessSecrec, $method = "GET") {
  106. ksort($params);
  107. $stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';
  108. $tmp = "";
  109. foreach($params as $key => $val){
  110. $tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
  111. }
  112. $tmp = trim($tmp, '&');
  113. $stringToSign = $stringToSign.$this->percentEncode($tmp);
  114. $key = $accessSecrec.'&';
  115. $hmac = hash_hmac("sha1", $stringToSign, $key, true);
  116. return base64_encode($hmac);
  117. }
  118. private function percentEncode($value = null){
  119. $en = urlencode($value);
  120. $en = str_replace("+", "%20", $en);
  121. $en = str_replace("*", "%2A", $en);
  122. $en = str_replace("%7E", "~", $en);
  123. return $en;
  124. }
  125. private function curl($url) {
  126. $ch = curl_init();
  127. curl_setopt($ch, CURLOPT_URL, $url );
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  129. $result = curl_exec ($ch);
  130. curl_close($ch);
  131. return $result;
  132. }
  133. private function out($msg) {
  134. return json_decode($msg, true);
  135. }
  136. }