Home > PHP > Pinging your sitemap to google after every edit or addition through a CMS with zend framework

Pinging your sitemap to google after every edit or addition through a CMS with zend framework

January 28th, 2009 Leave a comment Go to comments

This helpful action helper can be called from all edit and add actions from your CMS controllers. It checks if enough time has elapsed after the last ping, using Zend_Cache. It logs ping errors to the log through the logger that is initialized in the initializer (ZF >1.6 project layout). You can replace it with any action you like. The actual generation of the XML sitemap is not in the scope of this post.

 * This action helper pings google to index the changed urls through the sitemap.
 * @version 1.0
 * @uses Zend_Controller_Action_Helper_Abstract
require_once 'Zend/Controller/Action/Helper/Abstract.php';
require_once 'Zend/Cache.php';
require_once 'Zend/Date.php';
require_once 'Zend/Http/Client.php';
class Zend_Controller_Action_Helper_GoogleSitemapPing extends Zend_Controller_Action_Helper_Abstract
     * Executes the ping to google.
     * @return boolean Was google pinged or not?
    public function direct ()
        // Only submit sitemap if this is the production instance of this application.
        $env = Zend_Controller_Front::getInstance()->getPlugin('Initializer')->getEnv();
        if($env == 'production') {
            // Get sitemap publish cache.
            $cacheFrontendOptions = array('automatic_serialization' => true);
            $cacheBackendOptions = array('cache_dir' => APPLICATION_PATH . '/../tmp/googleSitemapPing');
            $cache = Zend_Cache::factory('Core', 'File', $cacheFrontendOptions, $cacheBackendOptions);
            $shouldPing = false;
            if(!($data = $cache->load('lastPing'))) {
                // Cache miss. Should ping.
                $shouldPing = true;
            } else {
                // Cache hit, check if enough time has elapsed since last ping.
                // May only submit sitemap once per hour. Once an hour is a bit much, lets make it two hours.
                if(Zend_Date::getTimestamp()-$data > 7200) {
                    // More than a hour has passed. Do ping to google.
                    $shouldPing = true;
            if($shouldPing) {
                // Do http request to the google sitemap ping frontend.
                // Setup new http client.
                $client = new Zend_Http_Client();
                $client->setConfig(array('timeout' => 10, 'useragent' => 'ZF Google sitemap pinger V1.0', 'maxredirects' => 2, 'keepalive' => true));
                $response = $client->request();
                if ($response->isSuccessful()) {
                    // Set new timestamp.
                    return true;
                } else {
                    // Ping not successful, add notice to error log.
                    $logger = Zend_Controller_Front::getInstance()->getPlugin('Initializer')->getLogger();
                    $logger->log('Could not ping google sitemap ping service!', Zend_Log::ERR); 
        return false;
  1. September 30th, 2012 at 04:41 | #1

    Goed punt !

  1. No trackbacks yet.