前言
为了绑定自定义域名到 Cloudflare Workers 再一次将域名解析托管到了 Cloudflare;
然而在导入解析记录时不知道为什么出现了一大堆,大概近 200 条了;
一条条删除太麻烦了就决定研究下 API 批量操作;
推荐:
「折腾」关于 2021 年末仍然没有完备的图床方案这件事_电脑网络_沉冰浮水
「折腾」Caddy 简易入门教程_电脑网络_沉冰浮水
「折腾」Git Submodule 探究_电脑网络_沉冰浮水
官方库和文档
cloudflare/cloudflare-php: PHP library for the Cloudflare v4 API:
https://github.com/cloudflare/cloudflare-php
Cloudflare PHP API Binding – Cloudflare Help Center:
https://support.cloudflare.com/hc/en-us/articles/115001661191
官方到是有 PHP 语言的 SDK,在其基础上封装了一个类方便使用;
这次这个东西和前不久折腾的 hexo 插件有一个共同点就是,它们的文档都让人痛苦;
然而过去和将来,也只能一次又一次的趟过这种痛苦,或者倒下吧。。
鉴权
API 令牌 | Cloudflare:https://dash.cloudflare.com/profile/api-tokens
↑ 实际用到的是账号邮箱和「Global API Key」,有全部权限的样子,「API 令牌」则用于细化权限,还不太清楚代码中怎么用;
相对通用的 class 封装
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
| require __DIR__ . "/vendor/autoload.php";
use Cloudflare\API\Auth\APIKey; use Cloudflare\API\Adapter\Guzzle; use Cloudflare\API\Endpoints\User; use Cloudflare\API\Endpoints\Zones; use Cloudflare\API\Endpoints\DNS;
class CloudFlare { public $errCode = 0; public $errMsg = ''; private $errInfos = array( 0 => '没有错误', 1 => '鉴权信息为空或错误 *' ); private $options = array(); private $oAPIKey = null; private $oAdapter = null; private $oUser = null; private $oZones = null; private $oDNS = null; public function __construct($options) { $this->options = $options; $bolOK = $this->checkOptions(); if ($bolOK) { $this->initAPI(); } } public function initUser() { if ($this->oUser !== null) { return $this->oUser; } $this->oUser = new User($this->oAdapter); return $this->oUser; } public function initZones() { if ($this->oZones !== null) { return $this->oZones; } $this->oZones = new Zones($this->oAdapter); return $this->oZones; } public function initDNS() { if ($this->oDNS !== null) { return $this->oDNS; } $this->oDNS = new DNS($this->oAdapter); return $this->oDNS; } public function listZones() { $oZones = $this->initZones(); return $oZones->listZones(); } public function listRecords($zoneID) { $oDNS = $this->initDNS(); return $oDNS->listRecords($zoneID); } public function deleteRecord($zoneID, $recordID) { $oDNS = $this->initDNS(); return $oDNS->deleteRecord($zoneID, $recordID); } private function initAPI() { if ($this->oAPIKey === null || $this->oAdapter === null) { $cf_email = $this->options['cf_email']; $cf_key = $this->options['cf_key']; $oAPIKey = new APIKey($cf_email, $cf_key); $oAdapter = new Guzzle($oAPIKey); $this->oAPIKey = $oAPIKey; $this->oAdapter = $oAdapter; } } private function checkOptions() { foreach ($this->options as $key => $value) { if (empty($value)) { $this->errCode = 1; $this->errMsg = "配置项 {$key} 不能为空"; return false; } } return true; } }
|
上层封装
因为界面和配置项什么的还是 Z-BlogPHP 比较熟悉,另外也可以糊个插件上架;
然而我自己就是想清空一波 DNS 解析,这插件有什么其他用途也是很迷;
以下仅为使用示意,Z-BlogPHP 插件版要不要上架也是略纠结;
甚至想着把 DNSPod 啥的也弄到一个插件里,以及半成品的插件要不要纳入到 Git 里;
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
| function CloudFlare_Init() { global $zbp; $cf_email = $zbp->Config('CloudFlare')->cf_email; $cf_key = $zbp->Config('CloudFlare')->cf_key; $cf_token = $zbp->Config('CloudFlare')->cf_token; $options = array( 'cf_email' => $cf_email, 'cf_key' => $cf_key, 'cf_token' => $cf_token, ); $cf = new CloudFlare($options); return $cf; }
function CloudFlare_ListDomins() { $cf = CloudFlare_Init(); if ($cf->errCode !== 0) { return "<td rolspan=\"3\">{$cf->errMsg}</td>"; } $tpl = file_get_contents(mz_DNMng_Path('cf_dm_list')); $rltHTML = ''; $listZones = $cf->listZones(); foreach ($listZones->result as $zone) { $rltHTML .= $tpl; $rltHTML = str_replace('{id}', $zone->id, $rltHTML); $rltHTML = str_replace('{name}', $zone->name, $rltHTML); $rltHTML = str_replace('{status}', $zone->status, $rltHTML); $rltHTML = str_replace('{plan_name}', $zone->plan->name, $rltHTML); } return $rltHTML; }
function CloudFlare_ListRecords($zoneID) { global $zbp; $cf = CloudFlare_Init(); if ($cf->errCode !== 0) { return "<td rolspan=\"3\">{$cf->errMsg}</td>"; } $tpl = file_get_contents(mz_DNMng_Path('cf_recd_list')); $csrfToken = $zbp->GetCSRFToken(); $rltHTML = ''; $listRecords = $cf->listRecords($zoneID); foreach ($listRecords->result as $record) { $rltHTML .= $tpl; $rltHTML = str_replace('{name}', $record->name, $rltHTML); $rltHTML = str_replace('{type}', $record->type, $rltHTML); $rltHTML = str_replace('{content}', $record->content, $rltHTML); $rltHTML = str_replace('{zone_id}', $record->zone_id, $rltHTML); $rltHTML = str_replace('{id}', $record->id, $rltHTML); $rltHTML = str_replace('{csrfToken}', $csrfToken, $rltHTML); } return $rltHTML; }
function CloudFlare_DelRecord($zoneID, $recordID) { $cf = CloudFlare_Init(); if ($cf->errCode !== 0) { return false; } $rlt = $cf->deleteRecord($zoneID, $recordID); return $rlt; }
function CloudFlare_ClearRecords($zoneID) { $cf = CloudFlare_Init(); if ($cf->errCode !== 0) { return false; } $listRecords = $cf->listRecords($zoneID); foreach ($listRecords->result as $record) { $zoneID = $record->zone_id; $recordID = $record->id; $cf->deleteRecord($zoneID, $recordID); } return $cf->errCode === 0 ? true : false; }
|