Cloudflare API ile kural aktif etme
14
●238
- 23-12-2024, 02:31:21https://github.com/cloudflare/cloudflare-php cloudflare php apisini kullanarak.lonescary adlı üyeden alıntı: mesajı görüntüle
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"; } - 23-12-2024, 14:11:03Hocam HTTP ERROR 500 hatası alıyorum bu koda erişmeye çalışınca.victories adlı üyeden alıntı: mesajı görüntüle
Fatal error: Uncaught Error: Call to undefined method GuzzleHttpClient::request() in /usr/www/users/fryt/rules.php:17 Stack trace: #0 {main} thrown in /usr/www/users/fryt/rules.php on line 17 - 23-12-2024, 22:45:04guzlle kurmanız gerekiyordu önce, curl ile yapalım biz
<?php // Cloudflare API kimlik bilgileri $apiKey = 'YOUR_API_KEY'; $email = 'YOUR_EMAIL'; $zoneId = 'YOUR_ZONE_ID'; // Kuralları listeleme fonksiyonu function listRules($apiKey, $email, $zoneId) { // CURL başlat $ch = curl_init("https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules"); // CURL ayarları curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Auth-Email: {$email}", "X-Auth-Key: {$apiKey}", "Content-Type: application/json" ]); // İsteği gönder $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // CURL hata kontrolü if(curl_errno($ch)) { echo "CURL Hatası: " . curl_error($ch) . "\n"; curl_close($ch); return; } curl_close($ch); // Yanıtı işle $result = json_decode($response, true); if ($httpCode == 200 && $result['success']) { if (empty($result['result'])) { echo "Hiç kural bulunamadı.\n"; return; } 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"; } else { echo "Hata oluştu (HTTP Kodu: $httpCode):\n"; print_r($result['errors'] ?? 'Bilinmeyen hata'); } } // Kural aktifleştirme fonksiyonu function activateRule($apiKey, $email, $zoneId, $ruleId) { // CURL başlat $ch = curl_init("https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules/{$ruleId}"); // İstek verisi $data = json_encode(['status' => 'active']); // CURL ayarları curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Auth-Email: {$email}", "X-Auth-Key: {$apiKey}", "Content-Type: application/json" ]); // İsteği gönder $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // CURL hata kontrolü if(curl_errno($ch)) { echo "CURL Hatası: " . curl_error($ch) . "\n"; curl_close($ch); return; } curl_close($ch); // Yanıtı işle $result = json_decode($response, true); if ($httpCode == 200 && $result['success']) { echo "Kural başarıyla aktif edildi.\n"; } else { echo "Hata oluştu (HTTP Kodu: $httpCode):\n"; print_r($result['errors'] ?? 'Bilinmeyen hata'); } } // Kullanım örnekleri: // Tüm kuralları listele echo "Kurallar listeleniyor...\n\n"; listRules($apiKey, $email, $zoneId); // Belirli bir kuralı aktif et // echo "\nKural aktif ediliyor...\n"; // activateRule($apiKey, $email, $zoneId, 'KURAL_ID');Örnek kullanım:
// Önce kuralları listele listRules($apiKey, $email, $zoneId); // Ardından istediğiniz kuralı aktif et activateRule($apiKey, $email, $zoneId, 'buraya_kural_id_gelecek');
- 23-12-2024, 23:23:23sorunsuz çalıştı hocam. kapatmak için ne kullanacağız passive mi disable mı?victories adlı üyeden alıntı: mesajı görüntüle
- 24-12-2024, 00:04:40şu şekilde değiştirebilirsiniz, önceki mesajdaki listRules fonksiyonunu eğer id leri biliyorsanız buna eklemenize gerek yok.
<?php // Cloudflare API kimlik bilgileri $apiKey = 'YOUR_API_KEY'; $email = 'YOUR_EMAIL'; $zoneId = 'YOUR_ZONE_ID'; // Kural durumunu değiştirme fonksiyonu function changeRuleStatus($apiKey, $email, $zoneId, $ruleId, $status = 'disabled') { // CURL başlat $ch = curl_init("https://api.cloudflare.com/client/v4/zones/{$zoneId}/firewall/rules/{$ruleId}"); // İstek verisi $data = json_encode(['status' => $status]); // CURL ayarları curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Auth-Email: {$email}", "X-Auth-Key: {$apiKey}", "Content-Type: application/json" ]); // İsteği gönder $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // CURL hata kontrolü if(curl_errno($ch)) { echo "CURL Hatası: " . curl_error($ch) . "\n"; curl_close($ch); return; } curl_close($ch); // Yanıtı işle $result = json_decode($response, true); if ($httpCode == 200 && $result['success']) { $statusText = $status == 'active' ? 'aktif' : 'pasif'; echo "Kural başarıyla {$statusText} edildi.\n"; } else { echo "Hata oluştu (HTTP Kodu: $httpCode):\n"; print_r($result['errors'] ?? 'Bilinmeyen hata'); } } // Kullanım örnekleri: // Kuralı pasif et // changeRuleStatus($apiKey, $email, $zoneId, 'KURAL_ID', 'disabled'); // Kuralı aktif et // changeRuleStatus($apiKey, $email, $zoneId, 'KURAL_ID', 'active'); - 24-12-2024, 00:17:26victories adlı üyeden alıntı: mesajı görüntüle
hocam kuralı pasif hale getiremiyor.