Namesilo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = Curl::send(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(self::$systemConfig['crash_warning_email'], '[Namesilo API] - [' . $operation . ']', $content, 0, $result['namesilo']['reply']['detail']);
  76. } else {
  77. Helpers::addEmailLog(self::$systemConfig['crash_warning_email'], '[Namesilo API] - [' . $operation . ']', $content, 1, $result['namesilo']['reply']['detail']);
  78. }
  79. return $result['namesilo']['reply'];
  80. } catch (\Exception $e) {
  81. Log::error('CURL请求失败:' . $e->getMessage() . ' --- ' . $e->getLine());
  82. Helpers::addEmailLog(self::$systemConfig['crash_warning_email'], '[Namesilo API] - [' . $operation . ']', $content, 0, $e->getMessage());
  83. return false;
  84. }
  85. }
  86. }