Namesilo.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\EmailLog;
  5. use LSS\XML2Array;
  6. use Log;
  7. class Namesilo
  8. {
  9. protected static $config;
  10. function __construct()
  11. {
  12. self::$config = $this->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::$config['namesilo_key']
  67. 'key' => '12d47532c95c7013b1ecc9a06'
  68. ];
  69. $query = array_merge($params, $data);
  70. $content = '请求操作:[' . $operation . '] --- 请求数据:[' . http_build_query($query) . ']';
  71. try {
  72. $result = $this->curlRequest('https://www.namesilo.com/api/' . $operation . '?' . http_build_query($query));
  73. $result = XML2Array::createArray($result);
  74. // 出错
  75. if (empty($result['namesilo']) || $result['namesilo']['reply']['code'] != 300 || $result['namesilo']['reply']['detail'] != 'success') {
  76. $this->sendEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $result['namesilo']['reply']['detail']);
  77. }
  78. return $result['namesilo']['reply'];
  79. } catch (\Exception $e) {
  80. Log::error('CURL请求失败:' . $e->getMessage() . ' --- ' . $e->getLine());
  81. $this->sendEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $e->getMessage());
  82. return false;
  83. }
  84. }
  85. /**
  86. * 写入邮件发送日志
  87. *
  88. * @param int $user_id 用户ID
  89. * @param string $title 标题
  90. * @param string $content 内容
  91. * @param int $status 投递状态
  92. * @param string $error 投递失败时记录的异常信息
  93. */
  94. private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  95. {
  96. $emailLogObj = new EmailLog();
  97. $emailLogObj->user_id = $user_id;
  98. $emailLogObj->title = $title;
  99. $emailLogObj->content = $content;
  100. $emailLogObj->status = $status;
  101. $emailLogObj->error = $error;
  102. $emailLogObj->created_at = date('Y-m-d H:i:s');
  103. $emailLogObj->save();
  104. }
  105. // 系统配置
  106. private function systemConfig()
  107. {
  108. $config = Config::query()->get();
  109. $data = [];
  110. foreach ($config as $vo) {
  111. $data[$vo->name] = $vo->value;
  112. }
  113. return $data;
  114. }
  115. // 发起一个CURL请求
  116. private function curlRequest($url, $data = null)
  117. {
  118. $ch = curl_init();
  119. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  121. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  122. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  124. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  125. curl_setopt($ch, CURLOPT_URL, $url);
  126. // 如果data有数据,则用POST请求
  127. if ($data) {
  128. curl_setopt($ch, CURLOPT_POST, 1);
  129. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  130. }
  131. $res = curl_exec($ch);
  132. curl_close($ch);
  133. return $res;
  134. }
  135. }