Namesilo.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Components;
  3. use LSS\XML2Array;
  4. use Log;
  5. class Namesilo
  6. {
  7. protected static $host;
  8. protected static $systemConfig;
  9. function __construct()
  10. {
  11. self::$host = 'https://www.namesilo.com/api/';
  12. self::$systemConfig = Helpers::systemConfig();
  13. }
  14. // 列出账号下所有域名
  15. public function listDomains()
  16. {
  17. return $this->send('listDomains');
  18. }
  19. // 列出指定域名的所有DNS记录
  20. public function dnsListRecords($domain)
  21. {
  22. $query = [
  23. 'domain' => $domain
  24. ];
  25. return $this->send('dnsListRecords', $query);
  26. }
  27. // 为指定域名添加DNS记录
  28. public function dnsAddRecord($domain, $host, $value, $type = 'A', $ttl = 7207)
  29. {
  30. $query = [
  31. 'domain' => $domain,
  32. 'rrtype' => $type,
  33. 'rrhost' => $host,
  34. 'rrvalue' => $value,
  35. 'rrttl' => $ttl
  36. ];
  37. return $this->send('dnsAddRecord', $query);
  38. }
  39. // 更新DNS记录
  40. public function dnsUpdateRecord($domain, $id, $host, $value, $ttl = 7207)
  41. {
  42. $query = [
  43. 'domain' => $domain,
  44. 'rrid' => $id,
  45. 'rrhost' => $host,
  46. 'rrvalue' => $value,
  47. 'rrttl' => $ttl
  48. ];
  49. return $this->send('dnsUpdateRecord', $query);
  50. }
  51. // 删除DNS记录
  52. public function dnsDeleteRecord($domain, $id)
  53. {
  54. $data = [
  55. 'domain' => $domain,
  56. 'rrid' => $id
  57. ];
  58. return $this->send('dnsDeleteRecord', $data);
  59. }
  60. // 发送请求
  61. private function send($operation, $data = [])
  62. {
  63. $params = [
  64. 'version' => 1,
  65. 'type' => 'xml',
  66. 'key' => self::$systemConfig['namesilo_key']
  67. ];
  68. $query = array_merge($params, $data);
  69. $content = '请求操作:[' . $operation . '] --- 请求数据:[' . http_build_query($query) . ']';
  70. try {
  71. $result = $this->curlRequest(self::$host . $operation . '?' . http_build_query($query));
  72. $result = XML2Array::createArray($result);
  73. // 出错
  74. if (empty($result['namesilo']) || $result['namesilo']['reply']['code'] != 300 || $result['namesilo']['reply']['detail'] != 'success') {
  75. Helpers::addEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $result['namesilo']['reply']['detail']);
  76. }
  77. return $result['namesilo']['reply'];
  78. } catch (\Exception $e) {
  79. Log::error('CURL请求失败:' . $e->getMessage() . ' --- ' . $e->getLine());
  80. Helpers::addEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $e->getMessage());
  81. return false;
  82. }
  83. }
  84. // 发起一个CURL请求
  85. private function curlRequest($url, $data = [])
  86. {
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  90. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  91. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  93. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  94. curl_setopt($ch, CURLOPT_URL, $url);
  95. // 如果data有数据,则用POST请求
  96. if ($data) {
  97. curl_setopt($ch, CURLOPT_POST, 1);
  98. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  99. }
  100. $res = curl_exec($ch);
  101. curl_close($ch);
  102. return $res;
  103. }
  104. }