Cloudflare api ile yapmak için, hesabınızda ekli olan sitelerin zone id'lerine ihtiyacınız olacak.

Buyrun tüm bu işlemleri yapan bir php sınıf. Bir php dosyası oluşturup içine bu kodu yapıştırın ve kullanım örneğindeki verileri kendinize göre ayarlayın.

class CloudflareDNSUpdater {
private $apiKey;
private $email;
private $newIP;
private $baseURL = 'https://api.cloudflare.com/client/v4/';

public function __construct($apiKey, $email, $newIP) {
$this->apiKey = $apiKey;
$this->email = $email;
$this->newIP = $newIP;
}

private function makeRequest($endpoint, $method = 'GET', $data = null) {
$ch = curl_init($this->baseURL . $endpoint);

$headers = [
'X-Auth-Email: ' . $this->email,
'X-Auth-Key: ' . $this->apiKey,
'Content-Type: application/json'
        ];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if ($method === 'PUT' || $method === 'PATCH') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
}

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return [
'code' => $httpCode,
'data' => json_decode($response, true)
];
}

public function getAllZones() {
$response = $this->makeRequest('zones');
if ($response['code'] === 200) {
return $response['data']['result'];
}
return null;
}

public function getDNSRecords($zoneId) {
$response = $this->makeRequest("zones/{$zoneId}/dns_records");
if ($response['code'] === 200) {
return $response['data']['result'];
}
return null;
}

public function updateDNSRecord($zoneId, $recordId, $recordData) {
$data = array_merge($recordData, ['content' => $this->newIP]);
return $this->makeRequest("zones/{$zoneId}/dns_records/{$recordId}", 'PATCH', $data);
}

public function updateAllARecords() {
$zones = $this->getAllZones();
if (!$zones) {
echo "Zoneler alınamadı!\n";
return false;
}

foreach ($zones as $zone) {
echo "İşleniyor: {$zone['name']}\n";

$dnsRecords = $this->getDNSRecords($zone['id']);
if (!$dnsRecords) {
echo "- DNS kayıtları alınamadı!\n";
continue;
}

foreach ($dnsRecords as $record) {
if ($record['type'] === 'A') {
$updateData = [
'type' => 'A',
'name' => $record['name'],
'proxied' => $record['proxied'],
'ttl' => $record['ttl']
];

$response = $this->updateDNSRecord($zone['id'], $record['id'], $updateData);

if ($response['code'] === 200) {
echo "- {$record['name']} güncellendi: {$this->newIP}\n";
} else {
echo "- {$record['name']} güncellenemedi!\n";
}
}
}
}
return true;
}
}

// Kullanım örneği:
$apiKey = 'YOUR_API_KEY';
$email = 'YOUR_EMAIL';
$newIP = 'NEW_IP_ADDRESS';

$updater = new CloudflareDNSUpdater($apiKey, $email, $newIP);
$updater->updateAllARecords();