txydns.php 7.7 KB

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