Home > Php > Verificar si un dominio esta disponible con php y json

En este post vamos a ver como realizar un script que me permita verificar la disponibilidad de los dominios, echo principalmente con PHP y utilizando un poco de ajax con Json jQuery.

Crearemos 2 archivos php y 1 css para darle un poco de stylo a nuestro ejemplo, tambien descargaremos la libreria jquery.

index.php
<html>
<head>
<title>Jquery Easy - Verificar si un dominio esta disponible con php y json</title>

<link href="main.css" rel="stylesheet" />
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>

<script language="javascript">
$(document).ready(function() {

	var loading;
	var results;
        var form;
	form = document.getElementById('form');
	loading = document.getElementById('loading');
	results = document.getElementById('results');

	$('#Submit').click( function() {

		if($('#Search').val() == "")
		{alert('Ingrese un dominio');return false;}

		results.style.display = 'none';
		$('#results').html('');
		loading.style.display = 'inline';

		$.post('process.php?domain=' + escape($('#Search').val()),{
		}, function(response){

			results.style.display = 'block';
			$('#results').html(unescape(response));
			loading.style.display = 'none';
		});

		return false;
	});

});
</script>
</head>
<body>
<div class="cabecera"><img src="images/logo.gif" /></div>

<h3 align="center">Verificar si un dominio esta disponible con php y json</h3>
<center>

	<form method="post" action="./" id="form">
	     <h3 style="color:#FFF">Ingrese solo el nombre del dominio (*sin prefijo .com, .org, etc)</h3>
		<input type="text" autocomplete="off" id="Search" name="domain">
		<input type="submit" id="Submit" value="Submit">

	</form>

	<div id="loading">Enviando datos....</div>

	 <div id="results" style="width:420px; height:600px;" align="left">

	 </div>	

 </center>
 </body>
 &lt;/html&gt;</pre>
En este archivo estamos diseñando nuestro formulario de consulta y estamos creando un metodo con jquery para validar y enviar los datos de mi formulario por ajax hacia el archivo que va aprocesar los datos.
<h5>process.php</h5>
<pre class="brush:php">&lt;?php
set_time_limit(0);
ob_start();

########### Extensiones
$extensions = array(
	'.com' 		=&gt; array('whois.crsnic.net','No match for'),
	'.info' 	=&gt; array('whois.afilias.net','NOT FOUND'),
	'.net' 		=&gt; array('whois.crsnic.net','No match for'),
	'.co.uk' 	=&gt; array('whois.nic.uk','No match'),
	'.nl' 		=&gt; array('whois.domain-registry.nl','not a registered domain'),
	'.ca' 		=&gt; array('whois.cira.ca', 'AVAIL'),
	'.name' 	=&gt; array('whois.nic.name','No match'),
	'.ws'		=&gt; array('whois.website.ws','No Match'),
	'.be' 		=&gt; array('whois.ripe.net','No entries'),
	'.org' 		=&gt; array('whois.pir.org','NOT FOUND'),
	'.biz' 		=&gt; array('whois.biz','Not found'),
	'.tv' 		=&gt; array('whois.nic.tv', 'No match for'),
);
###########

if(isset($_GET['domain']))
{
	$domain = str_replace(array('www.', 'http://','/'), NULL, $_GET['domain']);

	if(strlen($domain) &gt; 0)
	{
		foreach($extensions as $extension =&gt; $who)
		{
			$buffer = NULL;

			$sock = fsockopen($who[0], 43) or die('Error Connecting To Server:' . $server);
			fputs($sock, $domain.$extension . "\r\n");

				while( !feof($sock) )
				{
				  	$buffer .= fgets($sock,128);
				}

			fclose($sock);

			if(eregi($who[1], $buffer))
			{
				echo '&lt;h4 class="available"&gt;&lt;span&gt;Disponible&lt;/span&gt;' . $domain. '&lt;b&gt;' . $extension .'&lt;/b&gt; Esta Disponible&lt;/h4&gt;';
			}
			else
			{
				echo '&lt;h4 class="taken"&gt;&lt;span&gt;Tomado&lt;/span&gt;' . $domain . '&lt;b&gt;' .$extension .'&lt;/b&gt; Esta Tomado&lt;/h4&gt;';
			}
			echo '&lt;br /&gt;';	

			ob_flush();
			flush();
			sleep(0.3);

		}
	}
	else
	{
		echo 'Por favor ingrese nombre del dominio';
	}
}
?&gt;</pre>
Este archivo es el que recibe los parametros y realiza la busqueda del valor ingresado, creando un array con los prefijos de los dominios mas usados y nos devuelve los valores.
<h5>main.css</h5>
<pre class="brush:css">body {
	font-family: Tahoma, sans-serif;
	font-size: 12px;
	font-weight: bold; }

.cabecera {
	background: #4A3C31;
	border-bottom: 5px solid #69AD3C;
	margin: -8px 0 0 -8px;
	width: 100%; }

	.cabecera img { margin: 40px 0 0 30px; }

#loading { display: none; }

#form {
	width: 520px;
	background-color: #4A3C31;
	padding: 40px 50px 30px;
	margin: 10px 0;
	position: relative;
	border-radius: 15px;
	-moz-border-radius: 15px;
	-webkit-border-radius: 15px;
	-moz-border-radius: 15px 15px 15px 15px;
	border: solid 5px #69AD3C; }

#Search {
	border: medium none;
	color: #888888;
	float: left;
	font-family: Arial,Helvetica,Sans-serif;
	font-size: 15px;
	height: 36px;
	margin-right: 12px;
	outline: medium none;
	padding: 0 0 0 35px;
	text-shadow: 1px 1px 0 white;
	width: 425px; }

#Submit {
	background: url("Search.png") no-repeat scroll 0 0 transparent;
	border: medium none;
	cursor: pointer;
	height: 36px;
	overflow: hidden;
	text-indent: -999px;
	text-transform: uppercase;
	width: 83px; }

h4 {
	border: 2px solid #EEEEEE;
	font: 14px/1.3 Verdana,"Lucida Grande",Arial,Helvetica,Sans-Serif;
	margin: 0px;
	padding: 5px;
	min-width: 120px;
	text-align: left }

	h4.taken span { background: none repeat scroll 0 0 #F08F78; }

	h4.taken:hover { background: none repeat scroll 0 0 #FFC5B7; }

	h4 a { font-family:"crete-rounded-web-1","crete-rounded-web-2",sans-serif; color: #333333 }

	h4 span {
		font-family: Verdana;
		font-size: 12px;
		font-style: normal;
		margin-right: 4px;
		padding: 3px 5px; }

	h4.available:hover { background: none repeat scroll 0 0 #DDF2BC; }

	h4.available span { background: #bce67b; }

Esto es para darle un poco de vistosidad a nuestro ejercicio, espero que le sirva y cualquier duda pueden comentar.