Repository navigation
Expand file tree
/
Copy pathNamesilo.php
More file actions
125 lines (102 loc) · 3.38 KB
/
Copy pathNamesilo.php
File metadata and controls
125 lines (102 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace App\Components;
use LSS\XML2Array;
use Log;
class Namesilo
{
protected static $host;
protected static $systemConfig;
function __construct()
{
self::$host = 'https://www.namesilo.com/api/';
self::$systemConfig = Helpers::systemConfig();
}
// 列出账号下所有域名
public function listDomains()
{
return $this->send('listDomains');
}
// 列出指定域名的所有DNS记录
public function dnsListRecords($domain)
{
$query = [
'domain' => $domain
];
return $this->send('dnsListRecords', $query);
}
// 为指定域名添加DNS记录
public function dnsAddRecord($domain, $host, $value, $type = 'A', $ttl = 7207)
{
$query = [
'domain' => $domain,
'rrtype' => $type,
'rrhost' => $host,
'rrvalue' => $value,
'rrttl' => $ttl
];
return $this->send('dnsAddRecord', $query);
}
// 更新DNS记录
public function dnsUpdateRecord($domain, $id, $host, $value, $ttl = 7207)
{
$query = [
'domain' => $domain,
'rrid' => $id,
'rrhost' => $host,
'rrvalue' => $value,
'rrttl' => $ttl
];
return $this->send('dnsUpdateRecord', $query);
}
// 删除DNS记录
public function dnsDeleteRecord($domain, $id)
{
$data = [
'domain' => $domain,
'rrid' => $id
];
return $this->send('dnsDeleteRecord', $data);
}
// 发送请求
private function send($operation, $data = [])
{
$params = [
'version' => 1,
'type' => 'xml',
'key' => self::$systemConfig['namesilo_key']
];
$query = array_merge($params, $data);
$content = '请求操作:[' . $operation . '] --- 请求数据:[' . http_build_query($query) . ']';
try {
$result = $this->curlRequest(self::$host . $operation . '?' . http_build_query($query));
$result = XML2Array::createArray($result);
// 出错
if (empty($result['namesilo']) || $result['namesilo']['reply']['code'] != 300 || $result['namesilo']['reply']['detail'] != 'success') {
Helpers::addServerChanLog('[Namesilo API] - [' . $operation . ']', $content, 0, $result['namesilo']['reply']['detail']);
}
return $result['namesilo']['reply'];
} catch (\Exception $e) {
Log::error('CURL请求失败:' . $e->getMessage() . ' --- ' . $e->getLine());
Helpers::addServerChanLog('[Namesilo API] - [' . $operation . ']', $content, 0, $e->getMessage());
return false;
}
}
// 发起一个CURL请求
private function curlRequest($url, $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
// 如果data有数据,则用POST请求
if ($data) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}