Inicio > Programación Web > Generador PHP sitemap.xml

Generador PHP sitemap.xml

martes, 26 de marzo de 2024 Dejar un comentario Ir a comentarios

Cualquier pagina web debe utilizar un sitemap.xml, es realmente muy importante para los robots rastreen tu sitio correctamente.

Pero, ¿cómo hacer que funcione en PHP?

En primer lugar uso define’s para la ruta del archivo y el tiempo en que expirara:

define(SITEMAP_FILE,"sitemap.xml.gz");
define(SITEMAP_EXPIRE,3600); //segundos

Al comienzo de cualquier script se comprueba si el sitemap ha expirado, si ha expirado generamos otra vez el sitemap:

if (time()>(filemtime(SITEMAP_FILE)+SITEMAP_EXPIRE)){
	generateSitemap();// El sitemap esta expirado entonces lo generamos
}

Entonces usamos esta función para generar el archivo, cada vez que se necesite:

function generateSitemap(){//genera el sitemap retornando un xml
	$file=SITEMAP_FILE;
	$sitemap="<?xml version='1.0' encoding='UTF-8'?>
	<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
	xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
	xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9
			    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'>";
 
//to add url's:
	$sitemap.=makeUrlTag ("http://yoursite.com/rss/","", "hourly", "0.6");
//end adding urls	
 
	$sitemap.="</urlset>";
 
	if (file_exists($file))  unlink ($file);
	$gzdata = gzencode($sitemap, 9);
        $fp = fopen($file, "w");
        fwrite($fp, $gzdata);
        fclose($fp);
 
	return $sitemap;
}

Tambien necesitamos estas funciones de smart-it-consulting para hacer que todo funcione bien:

function makeUrlString ($urlString) {
    return htmlentities($urlString, ENT_QUOTES, 'UTF-8');
}
 
function makeIso8601TimeStamp ($dateTime) {
    if (!$dateTime) {
        $dateTime = date('Y-m-d H:i:s');
    }
    if (is_numeric(substr($dateTime, 11, 1))) {
        $isoTS = substr($dateTime, 0, 10) ."T"
                 .substr($dateTime, 11, 8) ."+00:00";
    }
    else {
        $isoTS = substr($dateTime, 0, 10);
    }
    return $isoTS;
}
 
function makeUrlTag ($url, $modifiedDateTime, $changeFrequency, $priority) {
    GLOBAL $newLine;
    GLOBAL $indent;
    GLOBAL $isoLastModifiedSite;
    $urlOpen = "$indent<url>$newLine";
    $urlValue = "";
    $urlClose = "$indent</url>$newLine";
    $locOpen = "$indent$indent<loc>";
    $locValue = "";
    $locClose = "</loc>$newLine";
    $lastmodOpen = "$indent$indent<lastmod>";
    $lastmodValue = "";
    $lastmodClose = "</lastmod>$newLine";
    $changefreqOpen = "$indent$indent<changefreq>";
    $changefreqValue = "";
    $changefreqClose = "</changefreq>$newLine";
    $priorityOpen = "$indent$indent<priority>";
    $priorityValue = "";
    $priorityClose = "</priority>$newLine";
 
    $urlTag = $urlOpen;
    $urlValue     = $locOpen .makeUrlString("$url") .$locClose;
    if ($modifiedDateTime) {
     $urlValue .= $lastmodOpen .makeIso8601TimeStamp($modifiedDateTime) .$lastmodClose;
     if (!$isoLastModifiedSite) { // ultima modificación del website
         $isoLastModifiedSite = makeIso8601TimeStamp($modifiedDateTime);
     }
    }
    if ($changeFrequency) {
     $urlValue .= $changefreqOpen .$changeFrequency .$changefreqClose;
    }
    if ($priority) {
     $urlValue .= $priorityOpen .$priority .$priorityClose;
    }
    $urlTag .= $urlValue;
    $urlTag .= $urlClose;
    return $urlTag;
}

Y eso es todo. 😉

Ahora puedes enviar tu sitemap.xml a google webmaster tools, haciendo ping a Google sobre los cambios en el mapa del sitio.

La manera mas sencilla de hacerlo es:

file_get_contents('http://www.google.com/webmasters/sitemaps/ping?sitemap=http://yoursite.com/sitemap.xml');

Recuerda que debe registrar el sitio en el google web tools, ya que sino no funcionará.

Comparte y diviertete:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • Blogosphere
  • Live
  • Meneame
  • MSN Reporter
  • MySpace
  • RSS
  • Suggest to Techmeme via Twitter
  • Technorati
  • LinkedIn
  • email
  • FriendFeed
  • PDF
  • Reddit
  • Wikio IT
  • Add to favorites
  • blogmarks
Top Footer