Jez Jez - 5 months ago 18
PHP Question

Api which checks domain validity

Can anyone refer me to a json api I can use to confirm the validity of websites users put into a form on my PHP website?

Answer

you do not need an api, you can check with a DNS query if the domain name really exists or not.

PHP checkdnsrr — Check DNS records corresponding to a given Internet host name or IP address

example of domain check:

<?php
function validate_domain($domain){

      //Check the DNS if the domain has an MX record
      if(checkdnsrr($domain,"MX")){
        return true;
      }else{
        return false;
      }
}
?>

example of email validation with reg exp + dns check

<?php
function validate_email($email){

   $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

   if(eregi($exp,$email)){

      if(checkdnsrr(array_pop(explode("@",$email)),"MX")){
        return true;
      }else{
        return false;
      }

   }else{

      return false;
   }    
}
?>

ref: http://php.net/manual/en/function.checkdnsrr.php