Namesilo.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Components;
  3. use Exception;
  4. use Log;
  5. use LSS\XML2Array;
  6. class Namesilo
  7. {
  8. protected static $host;
  9. protected static $systemConfig;
  10. function __construct()
  11. {
  12. self::$host = 'https://www.namesilo.com/api/';
  13. self::$systemConfig = Helpers::systemConfig();
  14. }
  15. // 列出账号下所有域名
  16. public function listDomains()
  17. {
  18. return $this->send('listDomains');
  19. }
  20. // 发送请求
  21. private function send($operation, $data = [])
  22. {
  23. $params = [
  24. 'version' => 1,
  25. 'type' => 'xml',
  26. 'key' => self::$systemConfig['namesilo_key']
  27. ];
  28. $query = array_merge($params, $data);
  29. $content = '请求操作:['.$operation.'] --- 请求数据:['.http_build_query($query).']';
  30. try{
  31. $result = Curl::send(self::$host.$operation.'?'.http_build_query($query));
  32. $result = XML2Array::createArray($result);
  33. // 出错
  34. if(empty($result['namesilo']) || $result['namesilo']['reply']['code'] != 300 || $result['namesilo']['reply']['detail'] != 'success'){
  35. Helpers::addNotificationLog('[Namesilo API] - ['.$operation.']', $content, 1, self::$systemConfig['webmaster_email'], 0, $result['namesilo']['reply']['detail']);
  36. }else{
  37. Helpers::addNotificationLog('[Namesilo API] - ['.$operation.']', $content, 1, self::$systemConfig['webmaster_email'], 1, $result['namesilo']['reply']['detail']);
  38. }
  39. return $result['namesilo']['reply'];
  40. }catch(Exception $e){
  41. Log::error('CURL请求失败:'.$e->getMessage().' --- '.$e->getLine());
  42. Helpers::addNotificationLog('[Namesilo API] - ['.$operation.']', $content, 1, self::$systemConfig['webmaster_email'], 0, $e->getMessage());
  43. return FALSE;
  44. }
  45. }
  46. // 列出指定域名的所有DNS记录
  47. public function dnsListRecords($domain)
  48. {
  49. $query = [
  50. 'domain' => $domain
  51. ];
  52. return $this->send('dnsListRecords', $query);
  53. }
  54. // 为指定域名添加DNS记录
  55. public function dnsAddRecord($domain, $host, $value, $type = 'A', $ttl = 7207)
  56. {
  57. $query = [
  58. 'domain' => $domain,
  59. 'rrtype' => $type,
  60. 'rrhost' => $host,
  61. 'rrvalue' => $value,
  62. 'rrttl' => $ttl
  63. ];
  64. return $this->send('dnsAddRecord', $query);
  65. }
  66. // 更新DNS记录
  67. public function dnsUpdateRecord($domain, $id, $host, $value, $ttl = 7207)
  68. {
  69. $query = [
  70. 'domain' => $domain,
  71. 'rrid' => $id,
  72. 'rrhost' => $host,
  73. 'rrvalue' => $value,
  74. 'rrttl' => $ttl
  75. ];
  76. return $this->send('dnsUpdateRecord', $query);
  77. }
  78. // 删除DNS记录
  79. public function dnsDeleteRecord($domain, $id)
  80. {
  81. $data = [
  82. 'domain' => $domain,
  83. 'rrid' => $id
  84. ];
  85. return $this->send('dnsDeleteRecord', $data);
  86. }
  87. }