mercoledì 9 novembre 2011

Typo3: pinging a google sitemap

If you give to a number of users to ability to post content on your Typo3 website, maybe you will feel the need to let google notice you have frequent changes on your site.

This is both a content indexing need and a raniking/positioning matter.

The basic operation to go with is have a google sitemap containting the changing pages to ping when new content appears.

So, i made a very simple sitemap and upload it through my google webmaster tools page. It contains a little number of pages, with the date of the last change.

Then, i inserted a little lumo php record on the landing page where the user is leaded after he posted the new content.

This little php thing do a pair things:

  1. check the actual google sitemap dates. if date match with today date, then job is already done... Exit! 
  2. update the google site map date with today date
  3. ping the sitemap
As you can see from point 1, i only ping the xml one time per day. No need to do it more times!
To archive point 3 i stole a chunk of code from the web.

Here's a sample code:
<?php
$filename = "googlesitemaptoping.xml";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$pos = strrpos($contents, date("Y-m-d"));
if ($pos === false) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = str_replace("DDDDDD", date("Y-m-d"), $contents);
$handle = fopen($filename, "w");
fwrite($handle, $contents);
fclose($handle);
/**
 * Function to ping Google Sitemaps.
 *
 * Function to ping Google Sitemaps. Returns an integer, e.g. 200 or 404,
 * 0 on error.
 *
 * @author     J de Silva                           <giddomains@gmail.com>
 * @copyright  Copyright &copy; 2005, J de Silva
 * @link       http://www.gidnetwork.com/b-54.html  PHP function to ping Google Sitemaps
 * @param      string   $url_xml  The sitemap url, e.g. http://www.example.com/google-sitemap-index.xml
 * @return     integer            Status code, e.g. 200|404|302 or 0 on error
 */
 $url_xml="http://www.yourwebsiteurl.org/".$filename;

   $status = 0;
   $google = 'www.google.com';
   if( $fp=@fsockopen($google, 80) )
   {
      $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 );
   }
}
?>
As usual, this will also work on any php enabled webserver.

Nessun commento:

Posta un commento