Merhabalar, bir php scripti yaptım. Bu scripte ek olarak cloudflare apisini kullanarak istediğim bir kuralı aktif etmek istiyorum bunu nasıl yapabilirim?
https://github.com/cloudflare/cloudflare-php cloudflare php apisini kullanarak.
mevcut kurallarınızı listeler.
<?php
require_once 'vendor/autoload.php';
// Cloudflare API kimlik bilgileri
$apiKey = 'YOUR_API_KEY';
$email = 'YOUR_EMAIL';
$zoneId = 'YOUR_ZONE_ID';
// API istemcisini oluştur
$client = new \GuzzleHttp\Client();
try {
// Tüm kuralları getir
$response = $client->request('GET', "https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules", [
'headers' => [
'X-Auth-Email' => $email,
'X-Auth-Key' => $apiKey,
'Content-Type' => 'application/json'
]
]);
$result = json_decode($response->getBody(), true);
if ($result['success']) {
if (empty($result['result'])) {
echo "Hiç kural bulunamadı.\n";
exit;
}
echo "MEVCUT KURALLAR:\n";
echo str_repeat('-', 100) . "\n";
echo sprintf("%-32s | %-20s | %-15s | %s\n", 'KURAL ID', 'AD', 'DURUM', 'AÇIKLAMA');
echo str_repeat('-', 100) . "\n";
foreach ($result['result'] as $rule) {
echo sprintf("%-32s | %-20s | %-15s | %s\n",
$rule['id'],
substr($rule['description'] ?? 'İsimsiz', 0, 20),
$rule['status'],
$rule['expression'] ?? 'Açıklama yok'
);
}
echo "\nToplam " . count($result['result']) . " kural listelendi.\n";
// JSON formatında kaydetme seçeneği
file_put_contents('cloudflare_rules.json', json_encode($result['result'], JSON_PRETTY_PRINT));
echo "\nDetaylı kural bilgileri 'cloudflare_rules.json' dosyasına kaydedildi.\n";
} else {
echo "Kurallar listelenirken hata oluştu:\n";
print_r($result['errors']);
}
} catch (\Exception $e) {
echo "Bir hata oluştu: " . $e->getMessage() . "\n";
}
// Belirli bir kuralın detaylarını görüntüleme fonksiyonu
function showRuleDetails($ruleId) {
global $client, $zoneId, $email, $apiKey;
try {
$response = $client->request('GET', "https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules/{$ruleId}", [
'headers' => [
'X-Auth-Email' => $email,
'X-Auth-Key' => $apiKey,
'Content-Type' => 'application/json'
]
]);
$result = json_decode($response->getBody(), true);
if ($result['success']) {
echo "\nKURAL DETAYLARI:\n";
echo str_repeat('-', 50) . "\n";
echo "ID: " . $result['result']['id'] . "\n";
echo "Açıklama: " . ($result['result']['description'] ?? 'Yok') . "\n";
echo "Durum: " . $result['result']['status'] . "\n";
echo "İfade: " . $result['result']['expression'] . "\n";
echo "Aksiyon: " . $result['result']['action'] . "\n";
if (isset($result['result']['products'])) {
echo "Ürünler: " . implode(', ', $result['result']['products']) . "\n";
}
}
} catch (\Exception $e) {
echo "Kural detayları alınırken hata oluştu: " . $e->getMessage() . "\n";
}
}
// Eğer belirli bir kuralın detaylarını görmek isterseniz:
// showRuleDetails('KURAL_ID');Yukarıdaki kod ile öğrendiğiniz KURAL_ID kullanarak aşağıdaki kodla aktif hale getirebilirsiniz.
<?php
require_once 'vendor/autoload.php';
// Cloudflare API kimlik bilgileri
$apiKey = 'YOUR_API_KEY';
$email = 'YOUR_EMAIL';
$zoneId = 'YOUR_ZONE_ID';
$ruleId = 'YOUR_RULE_ID'; // Aktif edilecek kuralın ID'si
// API istemcisini oluştur
$client = new \GuzzleHttp\Client();
try {
// Kuralı aktif etmek için PATCH isteği yap
$response = $client->request('PATCH', "https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules/{$ruleId}", [
'headers' => [
'X-Auth-Email' => $email,
'X-Auth-Key' => $apiKey,
'Content-Type' => 'application/json'
],
'json' => [
'status' => 'active'
]
]);
$result = json_decode($response->getBody(), true);
if ($result['success']) {
echo "Kural başarıyla aktif edildi.\n";
} else {
echo "Kural aktif edilirken hata oluştu:\n";
print_r($result['errors']);
}
} catch (\Exception $e) {
echo "Bir hata oluştu: " . $e->getMessage() . "\n";
}