Checking domain and nameservers in PHP script not working as expected

I’m working on a form in PHP that checks if a domain is available and if the nameservers are correct. However, I’m facing issues with the validation of the domain and nameservers.

<form id="domainForm">
    Subdomain: 
    <input type="text" id="subdomainInput" name="subdomain">
    Domain: 
    <select id="domainSelect" name="domain">
        <option value="faucet.lol">faucet.lol</option>
        <option value="freecrypto.tech">freecrypto.tech</option>
        <option value="custom">Custom</option>
    </select>
    <input type="text" id="customDomainInput" class="hidden" name="customDomain">
    <button type="button" onclick="submitForm()">Submit</button>
</form>

<script>
    var domainSelect = document.getElementById('domainSelect');
    var customDomainInput = document.getElementById('customDomainInput');
    domainSelect.addEventListener('change', function() {
        if (this.value === 'custom') {
            this.classList.add('hidden');
            customDomainInput.classList.remove('hidden');
        } else {
            this.classList.remove('hidden');
            customDomainInput.classList.add('hidden');
        }
    });

    function submitForm() {
        var isCustomDomain = domainSelect.value === 'custom';
        var domain = isCustomDomain ? customDomainInput.value : domainSelect.value;
        var subdomain = document.getElementById('subdomainInput').value;
        
        fetch('checkDomain.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: 'domain=' + encodeURIComponent(domain) + '&subdomain=' + encodeURIComponent(subdomain) + (isCustomDomain ? '&customDomain=' + encodeURIComponent(customDomainInput.value) : ''),
        })
        .then(response => response.text())
        .then(data => {
            if (data === 'OK') {
                var redirectUrl = isCustomDomain ? 
                    "https://faucethost.mysellix.io/product/65096ccda9171?Domain=" + encodeURIComponent(domain) + "&Subdomain=" + encodeURIComponent(subdomain) : 
                    "https://faucethost.mysellix.io/product/65096ccda9170?Domain=" + encodeURIComponent(domain) + "&Subdomain=" + encodeURIComponent(subdomain);
                window.location.href = redirectUrl;
            } else {
                alert(data);
            }
        });
    }
</script>
<?php
function isDomainAvailable($domain)
{
    if (!filter_var($domain, FILTER_VALIDATE_URL)) {
        return false;
    }

    $curlInit = curl_init($domain);
    curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curlInit, CURLOPT_HEADER, true);
    curl_setopt($curlInit, CURLOPT_NOBODY, true);
    curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($curlInit);

    curl_close($curlInit);

    if ($response) return true;

    return false;
}

function is_valid_domain_name($domain_name) {
    return (preg_match("/^([a-zd](-*[a-zd])*)(.([a-zd](-*[a-zd])*))*$/i", $domain_name) //valid characters check
    && preg_match("/^.{1,253}$/", $domain_name) //overall length check
    && preg_match("/^[^.]{1,63}(.[^.]{1,63})*$/", $domain_name) ); //length of every label
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $subdomain = $_POST['subdomain'];
    $domain = $_POST['domain'];

    if ($domain === 'custom') {
        $domain = $_POST['customDomain'];
    }

    if (!is_valid_domain_name($domain)) {
        echo "Error: Invalid domain name.";
        return;
    }

    if (isDomainAvailable('https://' . $subdomain . '.' . $domain)) {
        echo "Error: Subdomain already exists.";
        return;
    }

    if ($domain === 'custom') {
        $dnsRecords = dns_get_record($domain, DNS_NS);

        $correctNameservers = ['ns11.webshineglobal.xyz', 'ns12.webshineglobal.xyz'];
        $hasCorrectNameservers = false;

        foreach ($dnsRecords as $record) {
            if (in_array($record['target'], $correctNameservers)) {
                $hasCorrectNameservers = true;
                break;
            }
        }

        if (!$hasCorrectNameservers) {
            echo "Error: Please set your nameservers to ns11.webshineglobal.xyz and ns12.webshineglobal.xyz.";
            return;
        }
    }

    echo "OK";
    return;
}
?>

The issue I’m facing is that when a custom domain is selected, it always redirects the user and thinks the nameservers are correct. The only case it thinks the nameservers are incorrect is if I enter the word ‘custom’ into the domain field.

I have tried adding a domain validation function and printing out the $dnsRecords array to see what dns_get_record() is returning, but I’m still not able to identify where the issue is.

I would appreciate any guidance on how to troubleshoot and correct this issue. Thanks in advance.