Simple Google Site Map Generator : SAX : XML PHP Source Code


PHP Source Code » XML » SAX »

 

Simple Google Site Map Generator


Generate site page maps in Google site map format
<?php

    
/**
    * Project:        Google Site Map Generator
    *                
    * File:            GoogleSiteMap.php
    *
    * Copyright (C) 2006 DOUSSAU Alban
    * 
    *
    * @link http://www.dodecagone.com
    * @author DOUSSAU ALban <myalban at gmail dot com>
    * @version 1.0 29/05/2006
    */
    
    
class GoogleSiteMap {


    var 
$flux;            //XML String
    
var $nl;            //New Line
    
var $listfreq;        //Frequency List

    // Constructor function
    
function GoogleSiteMap(){
        
$this->Ini();
        
$this->StartMap();
    }

    
//------------------------------------------------------------
    //--------------------------PUBLIC FUNCTIONS------------------
    //------------------------------------------------------------
    
    
    // Add an url to the XML String
    // This function support string or array parameters but it's easy to extend it !
    
function AddURL($o){
        
        
$type=gettype($o);
        
//echo $type;
        
switch ($type){
            case 
"string":
                
$this->AddStringURL($o);
                break;
            case 
"array":
                
$this->AddArrayURL($o);
                break;
            default:
                break;
        }
        
    }
    
    
// Add a bunch of urls to the XML String
    // This function array parameters but it's easy to extend it !
    
    
function AddURLs($o){
        
$type=gettype($o);
        
//echo $type;
        
switch ($type){
            case 
"array":
                for (
$i=0;$i<count($o);$i++){
                    
$this->AddURL($o[$i]);
                }
                break;
            default:
                break;
        }
    }
    
    
// Close the XML String and return the XML String
    
function CloseMap(){
        
$this->flux.="</urlset>";
        
$this->GetFlux();
    }
    
    
// View the XML String in IE ( Or Firefox,....)
    
function ViewMap(){
        
header("Pragma: no-cache");
        
header("Content-Type: text/xml;charset=UTF-8");
        echo 
$this->flux;
    }
    
    
// Get the XML String
    
function GetFlux(){
        return 
$this->flux;
    }
    
    
//write the XML STRING in a specified file
    
function WriteFlux($file){
    
        
$this->MakerFile($file$this->flux);
    
    }
    
    
//------------------------------------------------------------
    //--------------------------PRIVATE FUNCTIONS-----------------
    //------------------------------------------------------------
    
    
    // Initialize
    
function Ini(){
        
$this->flux="";
        
$this->nl="\n";
        
$this->listfreq=array("always","hourly","daily","weekly","monthly","yearly","never");
        
    }
    
// Start the XML String
    
function StartMap(){
        
$this->flux.="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".$this->nl;
        
$this->flux.="<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd\">".$this->nl;
    }
    
    
// Add a simple url location to the XML String
    
function AddStringURL($s){
        
$this->flux.="<url>".$this->nl;
        
$this->flux.="<loc>".$this->CleanURL($s)."</loc>".$this->nl;
        
$this->flux.="</url>".$this->nl;
    }
    
// Add an url location with full option to the XML String
    
function AddArrayURL($a){
        
$this->flux.="<url>".$this->nl;
        while (list(
$k$v) = each($a)) {
                    switch(
$k) {
                        case 
"loc":
                            
$v=$this->CleanURL($v);
                            break;
                        case 
"lastmod":
                            
$v=($v=="")?date("c"):$v;
                            break;
                        case 
"changefreq":
                            
$v=(in_array($v,$this->listfreq))?$v:"monthly";
                            break;
                        case 
"priority":
                            
$v=($v==""||$v>1||$v<0)?"0.5":number_format($v,1);
                            break;
                    }
                            
                    
$this->flux.="<".$k.">".$v."</".$k.">".$this->nl;
        }
        
$this->flux.="</url>".$this->nl;
    
    }
    
    
    
// This function make a clean url with HTML ENTITIES end UTF-8 conversions
    
function CleanURL($s){
        
$s=htmlentities($s,ENT_QUOTES,"UTF-8");
        
$s=utf8_encode($s);
        return 
$s;
    }
    
    
//this function write a string to a file
    
    
function MakerFile($file$string) {
       
$f=fopen($file'w+');
       
ftruncate($f0);
       
fwrite($f$string);
       
fclose($f);
    }

    
}
    
?>



<?php //Example script

include_once "GoogleSiteMap.php";

// Create the GoogleSiteMap XML String
$map=new GoogleSiteMap();

//Add  simple URL
$map->AddURL("http://www.test.php?cat=boutique&id=5");

//Add an url with full option, but leave empty the not obligatory option.

$map->AddURL(array(loc=>"http://www.test.php?cat=boutique&id=6",lastmod=>"",changefreq=>"",priority=>""));

//Add another url with full option
$map->AddURL(array(loc=>"http://www.test.php?cat=boutique&id=7",lastmod=>"",changefreq=>"always",priority=>"0.2"));

//Add another url with full option
$map->AddURL(array(loc=>"http://www.test.php?cat=boutique&id=8",lastmod=>"1989-10-12",changefreq=>"never",priority=>"0.9"));

//But you can add multiple urls too
// We make an array of urls in string or array format ...
$a=array();
array_push($a,"http://www.test.php?cat=boutique&id=5");
array_push($a,array(loc=>"http://www.test.php?cat=boutique&id=6",lastmod=>"",changefreq=>"",priority=>""));
array_push($a,array(loc=>"http://www.test.php?cat=boutique&id=8",lastmod=>"1989-10-12",changefreq=>"never",priority=>"0.9"));
//...then we add them in the XML STRING
$map->AddURLs($a);


//Record the XML String
$result=$map->CloseMap();

//Output the XML String
$map->ViewMap();


//write the XML file to a specified file
//$map->WriteFlux("sitemap.xml");





?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo XML
» SAX