zend php and google webmaster tools api

update 2: Sandrine worked out a set of routines, as far as I know using Zend 1.7, she lists the code here.

update: Google updated their API in oktober (almost at the time I wrote these posts) and this code fails as it still based on the V1 APi. You can access the whole WT: toolset namespace (including sitemaps, verification) through the V2 API now, but you need to send a version id along with your request, that is handled in the new Zend 1.7 download.

The Problem

I can add 32.000 blogs on a standard WordPressMu install. How do I add 32.000 subdomains, verify them and add their sitemaps to Google Webmaster, without having to go to the webmaster page about 96.000 times ?

The solution

Integrating Google Webmaster Tools API into my WordPress Mu install.

What is it worth ?

If registering and verifying a site and adding a sitemap takes 5 minutes per domain, at E12,- per hour, that makes it 96.000 euros and 4 labor years for 32.000 sites. Writing a script is worth E96.000,- and saves me four years of mindless drone work, so that is well worth having a look at.

Software : Zend

Zend gData is a php framework that is programmed to handle Google Data. Their ClientLogin routine isn’t very flexible and they haven’t covered GWT Api yet, so I’ll have to hack some routines together.

After getting stonewalled by the zend program a few times, I went searching and ended up on ngoprekweb who have a nice post on ClientLogin authorization for the blogger api. Eris Ristemena uses a modified Zend ClientLogin, very nice work. I installed the adapted classes and tried that one to get through the ClientLogin, and it paid off.

The good stuff : Gwt api access

I am not interested in the blogger stuff though, I want access to GWT Google Webmaster Tools, so I worked Eris Ristemena’s blogger routine around a little.

set_include_path(dirname(__FILE__) . '/Zend_Gdata');
  1.   require_once 'Zend.php';
  2.   Zend::loadClass('Zend_Gdata_ClientLogin');
  3.   Zend::loadClass('Zend_Gdata');
  4.   Zend::loadClass('Zend_Feed');
  5.  
  6.   $username     = '';
  7.   $password     = '';
  8.   $service      = 'sitemaps';
  9.   $source       = 'Zend_ZendFramework-0.1.1'; // companyName-applicationName-versionID
  10.   $logintoken   = $_POST['captchatoken'];
  11.   $logincaptcha = $_POST['captchaanswer'];
  12.  
  13.   try {
  14.     $resp = Zend_Gdata_ClientLogin::getClientLoginAuth($username,$password,$service,$source,$logintoken,$logincaptcha);
  15.  
  16.     if ( $resp['response']=='authorized' )
  17.     {
  18.       $client = Zend_Gdata_ClientLogin::getHttpClient($resp['auth']);
  19.       $gdata = new Zend_Gdata($client);
  20.  
  21.    $feed = $gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/");
  22.          foreach ($feed as $item) {
  23.        echo '

  1. ';
  2.         }
  3.    }
  4.    elseif ( $resp['response']=='captcha' )
  5.    {
  6.      echo 'Google requires you to solve this CAPTCHA image';
  7.      echo '

';
  1.       echo '
‘; echo ‘Answer : ‘; echo ‘ ‘; echo ‘ ‘; echo ‘
  1. ';
  2.      exit;
  3.    }
  4.    else
  5.    {
  6.      // there is no way you can go here, some exceptions must have been thrown
  7.    }
  8.  
  9.  } catch ( Exception $e )  {
  10.    echo $e->getMessage();
  11.  }

(I added https://www.google.com/accounts/ to the captcha image source, otherwise it keeps drawing blanks.)

Zend uses a “HttpClient” for the connection to Google, and a gData class (usually the main ‘feed’, blogs, sites) that you use to do basic data manipulation. All feed entries are an atom format with a custom namespace.

Now I am going to add a domain. In my add_site function I put an XML Atom together to post (using the post() function of the gData class) to the sites feed url, and the Google API does the rest :

function add_site($domain, $client) {
  1.   $xml='';
  2.   $xml.='';
  3.   $xml.='';
  4.   $fdata = new Zend_Gdata($client);
  5.   $result=$fdata->post($xml,"https://www.google.com/webmasters/tools/feeds/sites/");
  6.   return $result;
  7. }

In the main routine I pass the domain and the running httpclient to the add_site() function :

if ( $resp['response']=='authorized' )
  1.     {
  2.       $client = Zend_Gdata_ClientLogin::getHttpClient($resp['auth']);
  3.       echo add_site('test.blacknorati.com', $client);
  4.     }

Cool. That saves me up to 32.000 site registrations. The rest of it is still greek to me, but this part functions. Next week : more nonsense (verify the site, add a sitemap, and integrate it in the blog creation function of wordpress mu).

1) about the blogger function : I tried to list the blogger posts with the ngoprekweb php code, but it seems blogger use a different string these days to identify the blog in gData, the id is returned as “tag:blogger.com-blabla-(blogid)” and you want the last part to access the blogs post atom feed :

$idText = split('-', $item->id());
  1.         $blogid = $idText[2];

(modified from the Zend 1.6.1 codebase)

foreach ($feed as $item) {
  1.         echo '<a href="'.$item->link(">' . $item-&gt;title() . '</a>';
  2.  
  3. $idText = split('-', $item-&gt;id());
  4.        $blogid = $idText[2];
  5.  
  6.        $feed1 = $gdata-&gt;getFeed("http://www.blogger.com/feeds/$blogid/posts/summary");
  7. //…
  8. }
Posted in php, tool, wordpress and tagged , , .