Skip to content
Derek Jones edited this page Jul 5, 2012 · 13 revisions

Category:Library::External | Category:Library::Sitemap

Introduction

Hello CI coders, this is my first contribution to CI so please bare with me on this one. Ok, this is a plugin to help on your website SEO, it helps your create an autocompressed (.gz) sitemap for google or others search engines. This plugin is based in a class from Svetoslav Marinov

Requirements

Google webmaster tools account

Download

File:google_sitemap_pi.zip

Source

google_sitemap_pi.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/** A class for generating simple google sitemaps
*@author Svetoslav Marinov <[email protected]>
*@copyright 2005
*@version 0.1
*@access public
*@package google_sitemap
*@link http://devquickref.com
*/
class google_sitemap
{
  var $header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t&lt;urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\"&gt;";
  var $charset = "UTF-8";
  var $footer = "\t</urlset>\n";
  var $items = array();

  /** Adds a new item to the channel contents.
   *@param google_sitemap item $new_item
   *@access public
   */
  function add_item($new_item){
    //Make sure $new_item is an 'google_sitemap item' object
    if(!is_a($new_item, "google_sitemap_item")){
      //Stop execution with an error message
      trigger_error("Can't add a non-google_sitemap_item object to the sitemap items array");
    }
    $this->items[] = $new_item;
  }

  /** Generates the sitemap XML data based on object properties.
   *@param string $file_name ( optional ) if file name is supplied the XML data is saved in it otherwise returned as a string.
   *@access public
   *@return [void|string]
   */
  function build( $file_name = null )
  {
    $map = $this->header . "\n";

    foreach($this->items as $item)
    {
        $item->loc = htmlentities($item->loc, ENT_QUOTES);
      $map .= "\t\t<url>\n\t\t\t<loc>$item->loc</loc>\n";

      // lastmod
      if ( !empty( $item->lastmod ) )
          $map .= "\t\t\t<lastmod>$item->lastmod</lastmod>\n";

      // changefreq
      if ( !empty( $item->changefreq ) )
          $map .= "\t\t\t<changefreq>$item->changefreq</changefreq>\n";

      // priority
      if ( !empty( $item->priority ) )
          $map .= "\t\t\t<priority>$item->priority</priority>\n";

      $map .= "\t\t</url>\n\n";
    }

    $map .= $this->footer . "\n";

    if(!is_null($file_name)){
      $fh = fopen&#40;$file_name, 'w'&#41;;
      fwrite($fh, $map);
      fclose($fh);
    }else{
      return $map;
    }
  }

}

/** A class for storing google_sitemap items and will be added to google_sitemap objects.
*@author Svetoslav Marinov <[email protected]>
*@copyright 2005
*@access public
*@package google_sitemap_item
*@link http://devquickref.com
*@version 0.1
*/
class google_sitemap_item
{
  /** Assigns constructor parameters to their corresponding object properties.
   *@access public
   *@param string $loc location
   *@param string $lastmod date (optional) format in YYYY-MM-DD or in "ISO 8601" format
   *@param string $changefreq (optional)( always,hourly,daily,weekly,monthly,yearly,never )
   *@param string $priority (optional) current link's priority ( 0.0-1.0 )
   */
  function google_sitemap_item( $loc, $lastmod = '', $changefreq = '', $priority = '' )
  {
    $this->loc = $loc;
    $this->lastmod = $lastmod;
    $this->changefreq = $changefreq;
    $this->priority = $priority;
  }
}
?&gt;

Usage

Ok, so now we will create a controller to make the sitemap.xml.gz and ping google to let him know that there's a new sitemap on our website for him to fetch and update googlebot. Of course you can autoload the plugin, but in our example let's justkeep it simple.

&lt;?php
class My_controller extends Controller
{
    function My_controller()
    {
        parent::Controller();
        $this->load->helper(array('text','url'));
        $this->load->plugin('google_sitemap'); //Load Plugin
    }

    function index()
    {
        $sitemap = new google_sitemap; //Create a new Sitemap Object
        $item = new google_sitemap_item(base_url()."MY_WEBSITE_URL",date("Y-m-d"), 'weekly', '0.8' ); //Create a new Item
        $sitemap->add_item($item); //Append the item to the sitemap object
        $sitemap->build("./sitemap.xml"); //Build it...
                
         //Let's compress it to gz
        $data = implode("", file&#40;"./sitemap.xml"&#41;);
        $gzdata = gzencode($data, 9);
        $fp = fopen&#40;"./sitemap.xml.gz", "w"&#41;;
        fwrite($fp, $gzdata);
        fclose($fp);

        //Let's Ping google
        $this->_pingGoogleSitemaps(base_url()."/sitemap.xml.gz");
    }

    function _pingGoogleSitemaps( $url_xml )
    {
       $status = 0;
       $google = 'www.google.com';
       if( $fp=@fsockopen&#40;$google, 80&#41; )
       {
          $req =  'GET /webmasters/sitemaps/ping?sitemap=' .
                  urlencode( $url_xml ) . " HTTP/1.1\r\n" .
                  "Host: $google\r\n" .
                  "User-Agent: Mozilla/5.0 (compatible; " .
                  PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
                  "Connection: Close\r\n\r\n";
          fwrite( $fp, $req );
          while( !feof($fp) )
          {
             if( @preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
             {
                $status = intval( $m[1] );
                break;
             }
          }
          fclose( $fp );
       }
       return( $status );
    }

}

As you can see, this is very simple to implement, you can fetch data from your database and create your urls on the fly, you can even create a cron job on your server to auto create the sitemap and ping google like:

wget http://your-host.com/my_controller/ , then your controller will generate a new sitemap.xml.gz and submit it to www.google.com//webmasters/sitemaps/ping?sitemap=http://your-host.com/sitemap.xml.gz
```I use this on my website and it's perfectly running, I hope this would help you in anyway.***PS:** Make sure you have permissions to read/write on ./sitemap.xml and ./sitemap.xml.gz*

Cheers,
Henrique Barroso
Clone this wiki locally