Jump to content

Webmaster Tools API Help needed


iainwood

Recommended Posts

Hi Guys

 

Has anyone had any experience using the webmaster tools API, I can't get my head around it. I have only used ZEND with pdf's before and not even that much then, it seems that they pitch ZEND as the way to do this, but I'm not able to find any relevant documentation (or am i missing something)

 

I have googled this to death, so please only tell me to go to a link if it isn't easily found on google, what I'm after is a simple exampple of how to say get a list of the sites on my account, I think I can figure it out from there!

 

Many thanks

 

Iain

Link to comment
Share on other sites

There isn't a service class for supporting the Google Webmaster Tools (GWT) API specifically.  The Google data protocol is supported by Zend Framework with Zend_GData.  GWT is based on 'feeds' or resources, whatever you wish to call them. 

 

Using a Feed with Google Data Protocol - From Fetching a Feed

    $gdata = new Zend_Gdata();
    $query = new Zend_Gdata_Query(
            'http://www.blogger.com/feeds/blogID/posts/default');
    $query->setMaxResults(10);
    $feed = $gdata->getFeed($query);

 

Of course with user specific data you'll need to authenticate before querying.

Link to comment
Share on other sites

Thanks for that, I'm trying to hack together something that works from the demo in the PHP files you can download from ZEND

 

I took the google Docs File and basically murdered it to try and find out what works. (some guesswork was involved),

This actually retrieves the site list- but I dont' know how?!?!

 

 

Authenticaion is being done first, then it retrieves the feed.

 

 

So step 1 getting a list of my sites from webmaster tools API with PHP is done, but pretty darn ugly, any takers on a simplified more elegant version of this?

 

 

 


<?php


require_once 'Zend/Loader.php';




Zend_Loader::loadClass('Zend_Gdata');


Zend_Loader::loadClass('Zend_Gdata_AuthSub');


Zend_Loader::loadClass('Zend_Gdata_ClientLogin');




Zend_Loader::loadClass('Zend_Gdata_Docs');






    session_start();






    if (!isset($_SESSION['docsSampleSessionToken']) && !isset($_GET['token'])) {
        requestUserLogin('Please login to your Google Account.');
    } else {
        $client = getAuthSubHttpClient();
	  $sites = new Zend_Gdata($client);
	 retrieveAllSites($sites, true);
	      }








function printSitesFeed($feed, $html)
{
	echo "Called - printSitesFeed <BR>";
  if ($html) {echo "<ul>\n";}


  // Iterate over the document entries in the feed and display each document's
  // title.
  foreach ($feed->entries as $entry) {


    if ($html) {
        // Find the URL of the HTML view of the document.
        $alternateLink = '';
        foreach ($entry->link as $link) {
            if ($link->getRel() === 'alternate') {
                $alternateLink = $link->getHref();
            }
        }
        // Make the title link to the document on docs.google.com.
        echo "<li><a href=\"$alternateLink\">\n";
    }


    echo "$entry->title\n";


    if ($html) {echo "</a></li>\n";}
  }


  if ($html) {echo "</ul>\n";}
}




function retrieveAllSites($client, $html)
{	echo "Called - retrieveAllSites <BR>";
  if ($html) {echo "<h2>Your documents</h2>\n";}




$feed = $client -> getFeed('https://www.google.com/webmasters/tools/feeds/sites/');
//print_r($feed);
// $feed = $client->getDocumentListFeed();


  printSitesFeed($feed, $html);
}




function requestUserLogin($linkText)
{
echo "Called - requestUserLogin <BR>";
    $authSubUrl = getAuthSubUrl();
    echo "<a href=\"{$authSubUrl}\">{$linkText}</a>";
}




function getAuthSubUrl()
{
	echo "Called - getAuthSubUrl <BR>";
    $next = getCurrentUrl();
    $scope = 'https://www.google.com/webmasters/tools/feeds/sites/';
    $secure = false;
    $session = true;
    return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,$session);
}




function getAuthSubHttpClient()
{
		echo "Called - getAuthSubHttpClient <BR>";
    global $_SESSION, $_GET;
    if (!isset($_SESSION['docsSampleSessionToken']) && isset($_GET['token'])) {
        $_SESSION['docsSampleSessionToken'] =
            Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
    }
    $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['docsSampleSessionToken']);
    print_r($client);
return $client;
}




function getCurrentUrl()
{
		echo "Called - getCurrentUrl <BR>";
    global $_SERVER;


    /**
     * Filter php_self to avoid a security vulnerability.
     */
    $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
    strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);


    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
        $protocol = 'https://';
    } else {
        $protocol = 'http://';
    }
    $host = $_SERVER['HTTP_HOST'];
    if ($_SERVER['SERVER_PORT'] != '' &&
        (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
        ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
            $port = ':' . $_SERVER['SERVER_PORT'];
    } else {
        $port = '';
    }
    return $protocol . $host . $port . $php_request_uri;
}



 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.