-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
160 lines (146 loc) · 5.63 KB
/
api.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class Api
{
// URL
private $url;
// email
private $email;
// apikey
private $apikey;
public function __construct($email, $apikey)
{
$this->url = 'https://api.cloudflare.com/client/v4/';
$this->email = $email;
$this->apikey = $apikey;
}
// 创建 DNS 记录
public function createDnsRecord(
$type = "A", // DNS record type.("A")
$name, // DNS record name (or @ for the zone apex)(example.com).
$content, // DNS record content.(127.0.0.1)
$ttl = 1, // Time to live, in seconds, of the DNS record. Must be between 60 and 86400, or 1 for 'automatic'.
$proxied, // Whether the record is receiving the performance and security benefits of Cloudflare.
$zoneId // zoneID
) {
$url = $this->url . 'zones/' . $zoneId . '/dns_records';
$data = [
'type' => $type,
'name' => $name,
'content' => $content,
'ttl' => $ttl,
'priority' => 10, // Required for MX, SRV and URI records; unused by other record types. Records with lower priorities are preferred.
'proxied' => $proxied == 1 ? true : false
];
return $this->curl($url, 'POST', $data);
}
// 更新 DNS 记录
public function updateDnsRecord(
$type = "A", // DNS record type.("A")
$name, // DNS record name (or @ for the zone apex)(example.com).
$content, // DNS record content.(127.0.0.1)
$ttl = 1, // Time to live, in seconds, of the DNS record. Must be between 60 and 86400, or 1 for 'automatic'.
$proxied, // Whether the record is receiving the performance and security benefits of Cloudflare.
$zoneId, // zoneID
$recordId // recordID
) {
$url = $this->url . 'zones/' . $zoneId . '/dns_records/' . $recordId;
$data = [
'type' => $type,
'name' => $name,
'content' => $content,
'ttl' => $ttl,
'proxied' => $proxied == 1 ? true : false,
];
return $this->curl($url, 'PUT', $data);
}
// 修补 DNS 记录
public function patchDnsRecord(
$type = "A", // DNS record type.("A")
$name, // DNS record name (or @ for the zone apex)(example.com).
$content, // DNS record content.(127.0.0.1)
$ttl = 1, // Time to live, in seconds, of the DNS record. Must be between 60 and 86400, or 1 for 'automatic'.
$proxied, // Whether the record is receiving the performance and security benefits of Cloudflare.
$zoneId, // zoneID
$recordId // recordID
) {
$url = $this->url . 'zones/' . $zoneId . '/dns_records/' . $recordId;
$data = [
'type' => $type,
'name' => $name,
'content' => $content,
'ttl' => $ttl,
'proxied' => $proxied == 1 ? true : false,
];
return $this->curl($url, 'PATCH', $data);
}
// 删除 DNS 记录
public function deleteDnsRecord($domain, $recordId)
{
$url = $this->url . 'zones/' . $this->getDomainZoneId($domain) . '/dns_records/' . $recordId;
return $this->curl($url, 'DELETE');
}
// 删除网站
public function deleteSite($domain)
{
$url = $this->url . 'zones/' . $this->getDomainZoneId($domain);
return $this->curl($url, 'DELETE');
}
// 获取区域
public function getZone($name)
{
$url = $this->url . 'zones';
$zone = $this->curl($url, 'POST', ['name' => $name]);
$zone = json_decode($zone, true);
return $zone;
}
// 获取指定域名 Record ID (@)
public function getDomainRecordId($domain)
{
return $this->matchDomainRecordId($domain);
}
// 获取指定域名 Record ID (www)
public function getDomainWwwRecordId($domain)
{
return $this->matchDomainRecordId($domain, 'www');
}
// 匹配域名,返回 Record ID
public function matchDomainRecordId($domain, $name = '')
{
$name = empty($name) ? '' : $name .= '.';
$url = $this->url . 'zones/' . $this->getDomainZoneId($domain) . '/dns_records?name=' . $name . $domain . '&match=all';
$res = $this->curl($url, 'GET');
$res = json_decode($res, true);
return $res['result'][0]['id'];
}
// 获取指定域名区域 ID
public function getDomainZoneId($domain)
{
$url = $this->url . 'zones?name=' . $domain;
$res = $this->curl($url, 'GET');
$res = json_decode($res, true);
return $res['result'][0]['id'];
}
// curl
public function curl($url, $method = "GET", $data = [])
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: ' . $this->email . '', // email
'X-Auth-Key: ' . $this->apikey . '', // apikey
'Cache-Control: no-cache',
'Content-Type:application/json'
));
if (!empty($data)) {
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
}
$sonuc = curl_exec($ch);
curl_close($ch);
return $sonuc;
}
}