PHP code example of tourze / ddns-contracts

1. Go to this page and download the library: Download tourze/ddns-contracts 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 / ddns-contracts example snippets




use Tourze\DDNSContracts\DNSProviderInterface;
use Tourze\DDNSContracts\DTO\ExpectResolveResult;

class MyDNSProvider implements DNSProviderInterface
{
    public function getName(): string
    {
        return 'my-dns-provider';
    }

    public function check(ExpectResolveResult $result): bool
    {
        // Check if this provider should handle the resolution
        return str_ends_with($result->getDomainName(), '.example.com');
    }

    public function resolve(ExpectResolveResult $result): void
    {
        // Implement DNS resolution logic
        $domain = $result->getDomainName();
        $ip = $result->getIpAddress();
        
        // Your DNS update logic here
    }
}

use Tourze\DDNSContracts\DNSProviderInterface;
use Tourze\DDNSContracts\DTO\ExpectResolveResult;

class CloudflareDNSProvider implements DNSProviderInterface
{
    public function getName(): string
    {
        return 'cloudflare';
    }

    public function check(ExpectResolveResult $result): bool
    {
        // Only handle domains managed by Cloudflare
        return $this->isCloudflareManaged($result->getDomainName());
    }

    public function resolve(ExpectResolveResult $result): void
    {
        // Update DNS record via Cloudflare API
        $this->updateCloudflareRecord(
            $result->getDomainName(),
            $result->getIpAddress()
        );
    }
}

use Tourze\DDNSContracts\IPResolverInterface;
use Tourze\DDNSContracts\DTO\ExpectResolveResult;

class StaticIPResolver implements IPResolverInterface
{
    public function resolveAll(): iterable
    {
        yield new ExpectResolveResult('example.com', '192.168.1.100');
        yield new ExpectResolveResult('subdomain.example.com', '192.168.1.101');
    }
}