Namesilo.php 2.7 KB

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