Namesilo.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 $host;
  10. protected static $systemConfig;
  11. function __construct()
  12. {
  13. self::$host = 'https://www.namesilo.com/api/';
  14. self::$systemConfig = Helpers::systemConfig();
  15. }
  16. // 列出账号下所有域名
  17. public function listDomains()
  18. {
  19. return $this->send('listDomains');
  20. }
  21. // 列出指定域名的所有DNS记录
  22. public function dnsListRecords($domain)
  23. {
  24. $query = [
  25. 'domain' => $domain
  26. ];
  27. return $this->send('dnsListRecords', $query);
  28. }
  29. // 为指定域名添加DNS记录
  30. public function dnsAddRecord($domain, $host, $value, $type = 'A', $ttl = 7207)
  31. {
  32. $query = [
  33. 'domain' => $domain,
  34. 'rrtype' => $type,
  35. 'rrhost' => $host,
  36. 'rrvalue' => $value,
  37. 'rrttl' => $ttl
  38. ];
  39. return $this->send('dnsAddRecord', $query);
  40. }
  41. // 更新DNS记录
  42. public function dnsUpdateRecord($domain, $id, $host, $value, $ttl = 7207)
  43. {
  44. $query = [
  45. 'domain' => $domain,
  46. 'rrid' => $id,
  47. 'rrhost' => $host,
  48. 'rrvalue' => $value,
  49. 'rrttl' => $ttl
  50. ];
  51. return $this->send('dnsUpdateRecord', $query);
  52. }
  53. // 删除DNS记录
  54. public function dnsDeleteRecord($domain, $id)
  55. {
  56. $data = [
  57. 'domain' => $domain,
  58. 'rrid' => $id
  59. ];
  60. return $this->send('dnsDeleteRecord', $data);
  61. }
  62. // 发送请求
  63. private function send($operation, $data = [])
  64. {
  65. $params = [
  66. 'version' => 1,
  67. 'type' => 'xml',
  68. 'key' => self::$systemConfig['namesilo_key']
  69. ];
  70. $query = array_merge($params, $data);
  71. $content = '请求操作:[' . $operation . '] --- 请求数据:[' . http_build_query($query) . ']';
  72. try {
  73. $result = $this->curlRequest(self::$host . $operation . '?' . http_build_query($query));
  74. $result = XML2Array::createArray($result);
  75. // 出错
  76. if (empty($result['namesilo']) || $result['namesilo']['reply']['code'] != 300 || $result['namesilo']['reply']['detail'] != 'success') {
  77. $this->addEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $result['namesilo']['reply']['detail']);
  78. }
  79. return $result['namesilo']['reply'];
  80. } catch (\Exception $e) {
  81. Log::error('CURL请求失败:' . $e->getMessage() . ' --- ' . $e->getLine());
  82. $this->addEmailLog(1, '[Namesilo API] - [' . $operation . ']', $content, 0, $e->getMessage());
  83. return false;
  84. }
  85. }
  86. /**
  87. * 写入邮件发送日志
  88. *
  89. * @param int $user_id 用户ID
  90. * @param string $title 标题
  91. * @param string $content 内容
  92. * @param int $status 投递状态
  93. * @param string $error 投递失败时记录的异常信息
  94. */
  95. private function addEmailLog($user_id, $title, $content, $status = 1, $error = '')
  96. {
  97. $emailLogObj = new EmailLog();
  98. $emailLogObj->user_id = $user_id;
  99. $emailLogObj->title = $title;
  100. $emailLogObj->content = $content;
  101. $emailLogObj->status = $status;
  102. $emailLogObj->error = $error;
  103. $emailLogObj->created_at = date('Y-m-d H:i:s');
  104. $emailLogObj->save();
  105. }
  106. // 发起一个CURL请求
  107. private function curlRequest($url, $data = [])
  108. {
  109. $ch = curl_init();
  110. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  111. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  112. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  113. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  114. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  116. curl_setopt($ch, CURLOPT_URL, $url);
  117. // 如果data有数据,则用POST请求
  118. if ($data) {
  119. curl_setopt($ch, CURLOPT_POST, 1);
  120. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  121. }
  122. $res = curl_exec($ch);
  123. curl_close($ch);
  124. return $res;
  125. }
  126. }