Namesilo.php 2.7 KB

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