Created
February 4, 2022 13:30
-
-
Save webuti/f05fdf670d093d1cb92fad8f4ecf3a54 to your computer and use it in GitHub Desktop.
Cloudflare api > add dns, remove dns,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CloudFlare | |
{ | |
const EMAIL = ""; | |
const API_KEY = ""; | |
const ENDPOINT = 'https://api.cloudflare.com/client/v4/zones'; | |
public $zoneIds = []; | |
public function __construct() | |
{ | |
$r = $this->getDomains(0); | |
foreach (range(0, $r['result_info']['total_pages']) as $page) { | |
$r = $this->getDomains($page); | |
foreach ($r['result'] as $domain) { | |
$this->zoneIds[$domain['name']] = $domain['id']; | |
} | |
} | |
} | |
public function getAllDomains() | |
{ | |
return $this->zoneIds; | |
} | |
public function request($url, $method = "GET", $data = "") | |
{ | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => self::ENDPOINT . $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => $method, | |
CURLOPT_POSTFIELDS => $data, | |
CURLOPT_HTTPHEADER => array( | |
'X-Auth-Email: ' . self::EMAIL, | |
'X-Auth-Key: ' . self::API_KEY, | |
'Content-Type: application/json', | |
), | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
return $response; | |
} | |
public function addDns($domain, $ip) | |
{ | |
$zoneId = $this->zoneIds[$domain]; | |
$this->request("/" . $zoneId . '/dns_records', 'POST', '{"type":"A","name":"@","content":"' . $ip . '","ttl":3600,"priority":10,"proxied":true}'); | |
} | |
public function addCnameDns($domain) | |
{ | |
$zoneId = $this->zoneIds[$domain]; | |
$this->request("/" . $zoneId . '/dns_records', 'POST', '{"type":"CNAME","name":"www","content":"' . $domain . '","ttl":3600,"priority":10,"proxied":true}'); | |
} | |
public function getDns($domain) | |
{ | |
$zoneId = $this->zoneIds[$domain]; | |
$response = $this->request("/" . $zoneId . '/dns_records', 'GET'); | |
$json = json_decode($response, TRUE); | |
$dnsArray = []; | |
foreach ($json['result'] as $dns) { | |
$dnsArray[] = [ | |
'id' => $dns['id'], | |
'name' => $dns['name'], | |
'content' => $dns['content'], | |
]; | |
} | |
return $dnsArray; | |
} | |
public function deleteAllDns($domain) | |
{ | |
$zoneId = $this->zoneIds[$domain]; | |
$getAll = $this->getDns($domain); | |
foreach ($getAll as $dnsId) { | |
$response = $this->request("/" . $zoneId . '/dns_records/' . $dnsId['id'], 'DELETE'); | |
} | |
} | |
public function deleteDns($domain, $dnsId) | |
{ | |
$zoneId = $this->zoneIds[$domain]; | |
$response = $this->request("/" . $zoneId . '/dns_records/' . $dnsId, 'DELETE'); | |
} | |
public function getDomains($page = 0) | |
{ | |
$response = $this->request('?page=' . $page, 'GET'); | |
return json_decode($response, TRUE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment