godaddydns.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. date_default_timezone_set("GMT");
  3. # 第一个参数是 action,代表 (add/clean)
  4. # 第二个参数是域名
  5. # 第三个参数是主机名(第三个参数+第二个参数组合起来就是要添加的 TXT 记录)
  6. # 第四个参数是 TXT 记录值
  7. # 第五个参数是 APPKEY
  8. # 第六个参数是 APPTOKEN
  9. echo "域名 API 调用开始\n";
  10. print_r($argv);
  11. if (count($argv) < 7) {
  12. echo "参数有误\n";
  13. exit;
  14. }
  15. echo $argv[1]."-".$argv[2]."-".$argv[3]."-".$argv[4]."-".$argv[5]."-".$argv[6]."\n";
  16. $domainarray = GodaddyDns::getDomain($argv[2]);
  17. $selfdomain = ($domainarray[0] == "") ? $argv[3] : $argv[3].".".$domainarray[0];
  18. /*
  19. $obj = new GodaddyDns($argv[5], $argv[6], $domainarray[1]);
  20. $data = $obj->getDomains();
  21. $data_obj = json_decode($data['result']);
  22. $code = $data['httpCode'];
  23. test :php godaddydns.php add yudadan.com v k
  24. */
  25. $obj = new GodaddyDns($argv[5], $argv[6], $domainarray[1]);
  26. switch ($argv[1]) {
  27. case "clean":
  28. //api 不包含该操作
  29. break;
  30. case "add":
  31. $data = $obj->GetDNSRecord($domainarray[1], $selfdomain);
  32. $data_obj = json_decode($data['result']);
  33. $count = count($data_obj);
  34. if ($count > 0) {
  35. $data = $obj->UpdateDNSRecord($domainarray[1], $selfdomain, $argv[4]);
  36. } else {
  37. $data = $obj->CreateDNSRecord($domainarray[1], $selfdomain, $argv[4]);
  38. }
  39. if ($data["httpCode"] != 200) {
  40. $message = json_decode($data["result"], true);
  41. echo "域名处理失败-".$message["message"];
  42. exit;
  43. }
  44. break;
  45. }
  46. echo "域名 API 调用结束\n";
  47. // $r = $obj->UpdateDNSRecord($domain, $selfdomain, $argv[3], $type);
  48. class GodaddyDns
  49. {
  50. private $accessKeyId = null;
  51. private $accessSecrec = null;
  52. private $DomainName = null;
  53. private $Host = "";
  54. private $Path = "";
  55. public function __construct($accessKeyId, $accessSecrec, $domain = "")
  56. {
  57. $this->accessKeyId = $accessKeyId;
  58. $this->accessSecrec = $accessSecrec;
  59. $this->DomainName = $domain;
  60. }
  61. /*
  62. 根据域名返回主机名和二级域名
  63. */
  64. public static function getDomain($domain)
  65. {
  66. //常见根域名 【https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains】
  67. // 【http://www.seobythesea.com/2006/01/googles-most-popular-and-least-popular-top-level-domains/】
  68. $arr[] = ".co.jp";
  69. $arr[] = ".com.tw";
  70. $arr[] = ".net";
  71. $arr[] = ".com";
  72. $arr[] = ".com.cn";
  73. $arr[] = ".org";
  74. $arr[] = ".cn";
  75. $arr[] = ".gov";
  76. $arr[] = ".net.cn";
  77. $arr[] = ".io";
  78. $arr[] = ".top";
  79. $arr[] = ".me";
  80. $arr[] = ".int";
  81. $arr[] = ".edu";
  82. $arr[] = ".link";
  83. $arr[] = ".uk";
  84. $arr[] = ".hk";
  85. //二级域名
  86. $seconddomain = "";
  87. //子域名
  88. $selfdomain = "";
  89. //根域名
  90. $rootdomain = "";
  91. foreach ($arr as $k => $v) {
  92. $pos = stripos($domain, $v);
  93. if ($pos) {
  94. $rootdomain = substr($domain, $pos);
  95. $s = explode(".", substr($domain, 0, $pos));
  96. $seconddomain = $s[count($s) - 1].$rootdomain;
  97. for ($i = 0; $i < count($s) - 1; $i++)
  98. $selfdomain .= $s[$i];
  99. break;
  100. }
  101. }
  102. //echo $seconddomain ;exit;
  103. if ($rootdomain == "") {
  104. $seconddomain = $domain;
  105. $selfdomain = "";
  106. }
  107. return array($selfdomain, $seconddomain);
  108. }
  109. public function error($code, $str)
  110. {
  111. echo "操作错误:".$code.":".$str;
  112. exit;
  113. }
  114. private function curl($url, $header = '', $data = '', $method = 'get')
  115. {
  116. $ch = curl_init();
  117. curl_setopt($ch, CURLOPT_URL, $url);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  119. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
  120. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置提交的字符串
  122. $result = curl_exec($ch);
  123. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  124. curl_close($ch);
  125. return array(
  126. 'result' => $result,
  127. 'httpCode' => $httpCode
  128. );
  129. }
  130. private function out($msg)
  131. {
  132. return json_decode($msg, true);
  133. }
  134. public function getDomains()
  135. {
  136. $url = "https://api.godaddy.com/v1/domains";
  137. $header = ['accept: application/json', 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  138. return $this->curl($url, $header);
  139. }
  140. public function delRecords($domain)
  141. {
  142. $url = "https://api.godaddy.com/v1/domains/$domain";
  143. $header = ['accept: application/json', 'Content-Type: application/json',
  144. 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  145. return $this->curl($url, $header, '', 'delete');
  146. }
  147. public function GetDNSRecord($domain, $record, $recordType = 'TXT')
  148. {
  149. $url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/$record";
  150. $header = ['accept: application/json', 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  151. return $this->curl($url, $header);
  152. }
  153. public function UpdateDNSRecord($domain, $name, $value, $recordType = 'TXT')
  154. {
  155. $url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/$name";
  156. $header = ['accept: application/json', 'Content-Type: application/json',
  157. 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  158. $data = array(
  159. array(
  160. 'data' => $value,
  161. 'name' => $name,
  162. 'ttl' => 3600,
  163. 'type' => $recordType)
  164. );
  165. return $this->curl($url, $header, json_encode($data), 'put');
  166. }
  167. public function CreateDNSRecord($domain, $name, $value, $recordType = 'TXT')
  168. {
  169. $url = "https://api.godaddy.com/v1/domains/$domain/records";
  170. $header = ['accept: application/json', 'Content-Type: application/json',
  171. 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
  172. $data = array(
  173. array(
  174. 'data' => $value,
  175. 'name' => $name,
  176. 'ttl' => 3600,
  177. 'type' => $recordType)
  178. );
  179. return $this->curl($url, $header, json_encode($data), 'PATCH');
  180. }
  181. }