godaddydns.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. date_default_timezone_set("GMT");
  3. //accessKeyId 和 accessSecrec 在 https://developer.godaddy.com/getstarted 申请
  4. define("accessKeyId", "");
  5. define("accessSecrec", "");
  6. $type = 'TXT';
  7. $domainarray = GodaddyDns::getDomain($argv[1]);
  8. //证书申请域名
  9. $selfdomain = ($domainarray[0] == "") ? $argv[2] : $argv[2].".".$domainarray[0];
  10. //根域名
  11. $domain = $domainarray[1];
  12. $obj = new GodaddyDns(accessKeyId, accessSecrec, $domain);
  13. $data = $obj->GetDNSRecord($domain, $type);
  14. $code = $data['httpCode'];
  15. if ($code != 200) {
  16. echo 'code='.$code;
  17. echo '<br/>';
  18. echo $data['result'];
  19. exit;
  20. }
  21. $data_obj = json_decode($data['result']);
  22. $count = count($data_obj);
  23. if ($count <= 0) {
  24. $r = $obj->CreateDNSRecord($domain, $selfdomain, $argv[3], $type);
  25. } else {
  26. $r = $obj->UpdateDNSRecord($domain, $selfdomain, $argv[3], $type); //$domain,$name,$value,$recordType='TXT
  27. }
  28. class GodaddyDns
  29. {
  30. private $accessKeyId = null;
  31. private $accessSecrec = null;
  32. private $DomainName = null;
  33. private $Host = "";
  34. private $Path = "";
  35. public function __construct($accessKeyId, $accessSecrec, $domain = "")
  36. {
  37. $this->accessKeyId = $accessKeyId;
  38. $this->accessSecrec = $accessSecrec;
  39. $this->DomainName = $domain;
  40. }
  41. /*
  42. 根据域名返回主机名和二级域名
  43. */
  44. public static function getDomain($domain)
  45. {
  46. //常见根域名 【https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains】
  47. // 【http://www.seobythesea.com/2006/01/googles-most-popular-and-least-popular-top-level-domains/】
  48. $arr[] = ".co.jp";
  49. $arr[] = ".com.tw";
  50. $arr[] = ".net";
  51. $arr[] = ".com";
  52. $arr[] = ".com.cn";
  53. $arr[] = ".org";
  54. $arr[] = ".cn";
  55. $arr[] = ".gov";
  56. $arr[] = ".net.cn";
  57. $arr[] = ".io";
  58. $arr[] = ".top";
  59. $arr[] = ".me";
  60. $arr[] = ".int";
  61. $arr[] = ".edu";
  62. $arr[] = ".link";
  63. $arr[] = ".uk";
  64. $arr[] = ".hk";
  65. //二级域名
  66. $seconddomain = "";
  67. //子域名
  68. $selfdomain = "";
  69. //根域名
  70. $rootdomain = "";
  71. foreach ($arr as $k => $v) {
  72. $pos = stripos($domain, $v);
  73. if ($pos) {
  74. $rootdomain = substr($domain, $pos);
  75. $s = explode(".", substr($domain, 0, $pos));
  76. $seconddomain = $s[count($s) - 1].$rootdomain;
  77. for ($i = 0; $i < count($s) - 1; $i++)
  78. $selfdomain .= $s[$i];
  79. break;
  80. }
  81. }
  82. //echo $seconddomain ;exit;
  83. if ($rootdomain == "") {
  84. $seconddomain = $domain;
  85. $selfdomain = "";
  86. }
  87. return array($selfdomain, $seconddomain);
  88. }
  89. public function error($code, $str)
  90. {
  91. echo "操作错误:".$code.":".$str;
  92. exit;
  93. }
  94. private function curl($url, $header = '', $data = '', $method = 'get')
  95. {
  96. $ch = curl_init();
  97. curl_setopt($ch, CURLOPT_URL, $url);
  98. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  99. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
  100. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  101. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置提交的字符串
  102. $result = curl_exec($ch);
  103. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  104. curl_close($ch);
  105. return array(
  106. 'result' => $result,
  107. 'httpCode' => $httpCode
  108. );
  109. }
  110. private function out($msg)
  111. {
  112. return json_decode($msg, true);
  113. }
  114. public function GetDNSRecord($domain, $recordType = 'TXT')
  115. {
  116. $url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/_acme-challenge";
  117. $header = ['accept: application/json', 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  118. return $this->curl($url, $header);
  119. }
  120. public function UpdateDNSRecord($domain, $name, $value, $recordType = 'TXT')
  121. {
  122. $url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/$name";
  123. $header = ['accept: application/json', 'Content-Type: application/json',
  124. 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  125. $data = array(
  126. array(
  127. 'data' => $value,
  128. 'name' => $name,
  129. 'ttl' => 3600,
  130. 'type' => $recordType)
  131. );
  132. return $this->curl($url, $header, json_encode($data), 'put');
  133. }
  134. public function CreateDNSRecord($domain, $name, $value, $recordType = 'TXT')
  135. {
  136. $url = "https://api.godaddy.com/v1/domains/$domain/records";
  137. $header = ['accept: application/json', 'Content-Type: application/json',
  138. 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  139. $data = array(
  140. array(
  141. 'data' => $value,
  142. 'name' => $name,
  143. 'ttl' => 3600,
  144. 'type' => $recordType)
  145. );
  146. return $this->curl($url, $header, json_encode($data), 'PATCH');
  147. }
  148. }