Skip to content

Instantly share code, notes, and snippets.

@maplerock
Created November 15, 2022 12:24
Show Gist options
  • Save maplerock/085856a9b23fc83b7b0598b334c9794b to your computer and use it in GitHub Desktop.
Save maplerock/085856a9b23fc83b7b0598b334c9794b to your computer and use it in GitHub Desktop.
Export DNS Zone file from Cpanel using Laravel/HTTP client
//You'll need to create an API key on WHM (https://api.docs.cpanel.net/whm/tokens/)
$user = "";
$key = "";
$domain = "yourdomain.com";
$saveTo = "";
$url = "https://YOUR_SERVER_IP:2087/json-api/json-api/export_zone_files?api.version=1&zone={$domain}";
$response = Http::withHeaders([
'Authorization' => "whm {$user}:{$key}"
])->withOptions(["verify"=>false])->get($url);
$response_json = $response->json();
$zones = collect($response_json['data']['payload']);
$zone = $zones->where('zone',$domain)->first();
$content = base64_decode($zone['text_b64']);
$targetPath = "{$saveTo}/{$domain}.txt";
$file = fopen($targetPath, 'w');
fwrite($file, $content);
fclose($file);
echo "File saved to {$targetPath}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment