PHP code example of tourze / cloudflare-dns-bundle

1. Go to this page and download the library: Download tourze/cloudflare-dns-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / cloudflare-dns-bundle example snippets


// config/bundles.php
return [
    // Dependencies (auto-registered via BundleDependencyInterface)
    Tourze\DoctrineTimestampBundle\DoctrineTimestampBundle::class => ['all' => true],
    Tourze\DoctrineUserBundle\DoctrineUserBundle::class => ['all' => true],
    Tourze\DoctrineTrackBundle\DoctrineTrackBundle::class => ['all' => true],
    Tourze\DoctrineIndexedBundle\DoctrineIndexedBundle::class => ['all' => true],
    // Main bundle
    CloudflareDnsBundle\CloudflareDnsBundle::class => ['all' => true],
];

use CloudflareDnsBundle\Entity\IamKey;

$iamKey = new IamKey();
$iamKey->setName('Main Account');
$iamKey->setEmail('[email protected]');
$iamKey->setAccessKey('your-api-key');
$iamKey->setAccountId('your-account-id');

$entityManager->persist($iamKey);
$entityManager->flush();

use CloudflareDnsBundle\Service\DnsRecordService;
use CloudflareDnsBundle\Entity\DnsRecord;
use CloudflareDnsBundle\Enum\DnsRecordType;

class MyService
{
    public function __construct(
        private DnsRecordService $dnsRecordService
    ) {}

    public function createRecord(): void
    {
        $record = new DnsRecord();
        $record->setDomain($domain);
        $record->setType(DnsRecordType::A);
        $record->setRecord('subdomain.example.com');
        $record->setContent('192.168.1.1');
        $record->setTtl(3600);
        $record->setProxy(true);

        $this->dnsRecordService->syncToRemote($record);
    }
}

use CloudflareDnsBundle\Service\DnsDomainService;

$domains = $dnsDomainService->syncFromRemote($iamKey);
foreach ($domains as $domain) {
    echo $domain->getName() . ' - ' . $domain->getStatus()->value . PHP_EOL;
}

use CloudflareDnsBundle\EventListener\DnsRecordSyncListener;
use Doctrine\ORM\Events;

class MyListener
{
    public function postPersist($args): void
    {
        $entity = $args->getObject();
        if ($entity instanceof DnsRecord) {
            // Custom logic after DNS record creation
        }
    }
}

use CloudflareDnsBundle\Client\CloudflareHttpClient;

$operations = [
    'posts' => [$newRecord1, $newRecord2],
    'puts' => [['id' => $recordId, 'record' => $updatedRecord]],
    'deletes' => [$recordIdToDelete]
];

$client->batchDnsRecords($zoneId, $operations);
bash
# Sync DNS analytics for last 24 hours
php bin/console cloudflare:sync-dns-analytics

# Sync analytics for specific time range
php bin/console cloudflare:sync-dns-analytics --since="2024-01-01" --until="2024-01-31"