Namesilo.php 2.6 KB

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