txydns.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. date_default_timezone_set("GMT");
  3. ############ 请在腾讯云申请“API密钥”,替换下面两个常量
  4. //去 https://console.cloud.tencent.com/cam/capi 页面申请
  5. define("txyaccessKeyId", "");
  6. define("txyaccessSecrec", "");
  7. ######### 类测试
  8. /*
  9. $obj = new TxyDns(txyaccessKeyId, txyaccessSecrec, "yudadan.com");
  10. //显示所有域名
  11. //$obj->DomainList();
  12. //添加域名 TXT 记录
  13. $obj->RecordCreate("www3","TXT","s");
  14. //显示某个域名所有的 TXT 记录
  15. $data = $obj->RecordList("www3","TXT");
  16. */
  17. ###### 代码运行
  18. // php txydns.php "simplehttps.com" "txtname" "txtvalue"
  19. //$argv[1] = "simplehttps.com";
  20. //$argv[2] = "www3";
  21. //$argv[3] = "ssssss";
  22. $domainarray = TxyDns::getDomain($argv[1]);
  23. $selfdomain = ($domainarray[0]=="")?$argv[2]:$argv[2] . "." . $domainarray[0];
  24. //为了匹配出二级域名,以及正确的RR
  25. $obj = new TxyDns(txyaccessKeyId, txyaccessSecrec, $domainarray[1]);
  26. $data = $obj->RecordList($selfdomain , "TXT");
  27. if ($data["code"] != "0") {
  28. $obj->error($data["code"], $data["message"]);
  29. }
  30. $records = $data["data"]["records"];
  31. foreach ($records as $k => $v) {
  32. // 如果存在记录,则直接修改。
  33. if ($v["name"] == $selfdomain) {
  34. $data = $obj->RecordModify($selfdomain, "TXT", $argv[3], $v["id"]);
  35. if ($data["code"] != "0") {
  36. $obj->error($data["code"], $data["message"]);
  37. }
  38. //$obj->RecordDelete($v["id"]);
  39. exit;
  40. }
  41. }
  42. //如果不存在,就增加 TXT 记录
  43. $data = $obj->RecordCreate($selfdomain, "TXT", $argv[3]);
  44. if ($data["code"] != "0") {
  45. //失败,则记录日志
  46. $obj->error($data["code"], $data["message"]);
  47. }
  48. ####### 基于腾讯云 DNS API 实现的 PHP 类,参考 https://cloud.tencent.com/document/product/302/4032
  49. class TxyDns {
  50. private $accessKeyId = null;
  51. private $accessSecrec = null;
  52. private $DomainName = null;
  53. private $Host = "cns.api.qcloud.com";
  54. private $Path = "/v2/index.php";
  55. public function __construct($accessKeyId, $accessSecrec, $domain = "") {
  56. $this->accessKeyId = $accessKeyId;
  57. $this->accessSecrec = $accessSecrec;
  58. $this->DomainName = $domain;
  59. }
  60. /*
  61. 根据域名返回主机名和二级域名
  62. */
  63. public static function getDomain($domain) {
  64. //常见根域名 【https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains】
  65. // 【http://www.seobythesea.com/2006/01/googles-most-popular-and-least-popular-top-level-domains/】
  66. $arr[]=".uk";
  67. $arr[]=".hk";
  68. $arr[]=".net";
  69. $arr[]=".com";
  70. $arr[]=".edu";
  71. $arr[]=".mil";
  72. $arr[]=".com.cn";
  73. $arr[]=".org";
  74. $arr[]=".cn";
  75. $arr[]=".gov";
  76. $arr[]=".net.cn";
  77. $arr[]=".io";
  78. $arr[]=".co.jp";
  79. $arr[]=".com.tw";
  80. $arr[]=".info";
  81. $arr[]=".io";
  82. $arr[]=".top";
  83. $arr[]=".me";
  84. $arr[]=".int";
  85. $arr[]=".edu";
  86. //二级域名
  87. $seconddomain ="";
  88. //子域名
  89. $selfdomain = "";
  90. //根域名
  91. $rootdomain = "";
  92. foreach ($arr as $k=>$v) {
  93. $pos = stripos($domain,$v);
  94. if ($pos) {
  95. $rootdomain = substr($domain,$pos);
  96. $s = explode(".",substr($domain,0,$pos));
  97. $seconddomain = $s[count($s)-1] . $rootdomain;
  98. for ($i=0;$i<count($s)-1;$i++)
  99. $selfdomain .= $s[$i];
  100. break;
  101. }
  102. }
  103. //echo $seconddomain ;exit;
  104. if ($rootdomain=="") {
  105. $seconddomain = $domain;
  106. $selfdomain = "";
  107. }
  108. return array($selfdomain,$seconddomain);
  109. }
  110. public function error($code, $str) {
  111. echo "操作错误:" . $code . ":" . $str;
  112. exit;
  113. }
  114. public function RecordDelete($recordId) {
  115. $param["domain"] = $this->DomainName;
  116. $param["recordId"] = $recordId;
  117. $data = $this->send("RecordDelete", "GET", $param);
  118. return ($this->out($data));
  119. }
  120. public function RecordList($subDomain, $recordType = "") {
  121. if ($recordType != "")
  122. $param["recordType"] = $recordType;
  123. $param["subDomain"] = $subDomain;
  124. $param["domain"] = $this->DomainName;
  125. $data = $this->send("RecordList", "GET", $param);
  126. return ($this->out($data));
  127. }
  128. public function RecordModify($subDomain, $recordType = "TXT", $value, $recordId) {
  129. $param["recordType"] = $recordType;
  130. $param["subDomain"] = $subDomain;
  131. $param["recordId"] = $recordId;
  132. $param["domain"] = $this->DomainName;
  133. $param["recordLine"] = "默认";
  134. $param["value"] = $value;
  135. $data = $this->send("RecordModify", "GET", $param);
  136. return ($this->out($data));
  137. }
  138. public function RecordCreate($subDomain, $recordType = "TXT", $value) {
  139. $param["recordType"] = $recordType;
  140. $param["subDomain"] = $subDomain;
  141. $param["domain"] = $this->DomainName;
  142. $param["recordLine"] = "默认";
  143. $param["value"] = $value;
  144. $data = $this->send("RecordCreate", "GET", $param);
  145. return ($this->out($data));
  146. }
  147. public function DomainList() {
  148. $data = $this->send("DomainList", "GET", array());
  149. return ($this->out($data));
  150. }
  151. private function send($action, $reqMethod, $requestParams) {
  152. $params = $this->formatRequestData($action, $requestParams, $reqMethod);
  153. $uri = http_build_query($params);
  154. $url = "https://" . $this->Host . "" . $this->Path . "?" . $uri;
  155. return $this->curl($url);
  156. }
  157. private function formatRequestData($action, $request, $reqMethod) {
  158. $param = $request;
  159. $param["Action"] = ucfirst($action);
  160. //$param["RequestClient"] = $this->sdkVersion;
  161. $param["Nonce"] = rand();
  162. $param["Timestamp"] = time();
  163. //$param["Version"] = $this->apiVersion;
  164. $param["SecretId"] = $this->accessKeyId;
  165. $signStr = $this->formatSignString($this->Host, $this->Path, $param, $reqMethod);
  166. $param["Signature"] = $this->sign($signStr);
  167. return $param;
  168. }
  169. //签名
  170. private function formatSignString($host, $path, $param, $requestMethod) {
  171. $tmpParam = [];
  172. ksort($param);
  173. foreach ($param as $key => $value) {
  174. array_push($tmpParam, str_replace("_", ".", $key) . "=" . $value);
  175. }
  176. $strParam = join("&", $tmpParam);
  177. $signStr = strtoupper($requestMethod) . $host . $path . "?" . $strParam;
  178. return $signStr;
  179. }
  180. private function sign($signStr) {
  181. $signature = base64_encode(hash_hmac("sha1", $signStr, $this->accessSecrec, true));
  182. return $signature;
  183. }
  184. private function curl($url) {
  185. $ch = curl_init();
  186. curl_setopt($ch, CURLOPT_URL, $url);
  187. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  188. $result = curl_exec($ch);
  189. curl_close($ch);
  190. return $result;
  191. }
  192. private function out($msg) {
  193. return json_decode($msg, true);
  194. }
  195. }