GIDNetwork > PHP function to ping Google Sitemaps
Register
« Conversion: Binary, Decimal, Hexadecimal Command Line Arguments, Part 1 »

PHP function to ping Google Sitemaps

by: admin - Aug 29, 2005

Now that I have completed writing the custom PHP script that generates the Google sitemaps for GIDForums, I thought it would be helpful to share with you some of the functions that were included in the script.

One of the most useful, I think, is the pingGoogleSitemaps() function below. All it does is ping Google Sitemaps to let them know that your sitemap (or sitemap index) file has recently been updated. Just in case you need more information about submitting your sitemaps, you can read about it here: Submitting your Google Sitemaps.

Custom PHP function: pingGoogleSitemaps()

PHP Code Example:


<?php
/**
 * 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
 */
function pingGoogleSitemaps$url_xml )
{
   $status 0;
   $google 'www.google.com';
   if( $fp=@fsockopen($google80) )
   {
      $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($fp128), $m) )
         {
            $status intval$m[1] );
            break;
         }
      }
      fclose$fp );
   }
   return( $status );
}
\?>

I just want to point out this sentence from the Submitting your Google Sitemaps page:

... A successful request will return an HTTP 200 response code; if you receive a different response, you should resubmit your request...

Which is why this function simply returns the 3 digit HTTP status code at the end and nothing more. If for some strange reason, it fails to even connect to www.google.com however, it will just return 0.

Example use: pingGoogleSitemaps()

For GIDForums™, I set up a cron job to run daily, generating up to 3 (Google) sitemaps and 1 sitemap index file.

Let's assume the 3 sitemaps are:

  • http://www.gidforums.com/sitemap-forums.xml.gz (updated daily)

  • http://www.gidforums.com/sitemap-threads.xml.gz (updated weekly)

  • http://www.gidforums.com/sitemap-latest.xml.gz (updated daily)

When the cron job executes cron-sitemap-generator.php everyday, the script queries the database and generates the sitemap files accordingly. As soon as it's done writing to these files, it uses the same information to produce the sitemap INDEX file, filenamed: sitemap-index.xml. This INDEX file is what we need to ping to Google Sitemaps and we use this pingGoogleSitemaps() function to do that easily -- see the following example code:

PHP Code Example:



<?php
// FILENAME: cron-sitemap-generator.php
// ====================================

// php code that generates sitemaps and sitemap index files
// ...

// Once the sitemaps are ready, we ping Google...
if( 200 === ($status=pingGoogleSitemaps('http://www.gidforums.com/sitemap-index.xml')) )
   report"Ping to Google Sitemaps successful.\r\n\r\nStatus code: $status."REPORT_MODE );
else
   report"Cannot ping/connect to Google Sitemaps.\r\n\r\nStatus code: $status."REPORT_MODE );

// end the script.
?>


My personal experience with automated pings to Google Sitemaps is that it takes anywhere between 1 - 23 hours for Googlebot to come around and fetch the updated sitemaps and index file.

Would you like to comment? This story has been viewed 58,825 times.
« Conversion: Binary, Decimal, Hexadecimal Command Line Arguments, Part 1 »

__top__

Copyright © GIDNetwork™ 2001 - 2017

Another website by J de Silva

Page generated in : 0.01210 sec.