Skip to content

Instantly share code, notes, and snippets.

@marcofbb
Created June 5, 2022 02:05
Show Gist options
  • Save marcofbb/2b5ebed7e040264ca17fbaa85a361550 to your computer and use it in GitHub Desktop.
Save marcofbb/2b5ebed7e040264ca17fbaa85a361550 to your computer and use it in GitHub Desktop.
Cloudflare delete all DNS records and set news (MASSIVE PHP)
<?php
/*
RUN:
composer install cloudflare/sdk
*/
require_once('vendor/autoload.php');
$domains = array('domain1.com','domain2.com');
$key = new \Cloudflare\API\Auth\APIKey('useremail@email.com', 'ApiKey');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new \Cloudflare\API\Endpoints\Zones($adapter);
function console_log($txt){
echo $txt.PHP_EOL;
}
foreach($domains as $domain){
console_log('Domain: '.$domain);
$zoneId = $zones->getZoneID($domain);
if(empty($zoneId)){
console_log('ERROR: ZoneID vacia');
continue;
}
console_log('ZoneID: '.$zoneId);
$zonesDns = new \Cloudflare\API\Endpoints\DNS($adapter);
$resultDns = $zonesDns->listRecords($zoneId);
if(!empty($resultDns->result)){
foreach($resultDns->result as $r){
$zonesDns->deleteRecord($zoneId, $r->id);
console_log('Borrando DNS: '.$r->id);
}
}
$zonesDns->addRecord($zoneId, 'A', $domain, '10.0.0.1');
$zonesDns->addRecord($zoneId, 'A', 'www.'.$domain, '10.0.0.1');
console_log('DNS AGREGADOS');
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment