Draft class for Manipulating Performancing Data

Notice the top posts report generated with performancing data on the bottom part of my right navigation bar? I subscribed to a service called performancing metrics and used their free REST-based web services API to generate that nice report on the side. I created this simple class for interfacing with their web service.

<?php

class Performancing {
  var $url = "";
  var $base = "http://performancing.com/perfstats/api.php";
  var $key = "";
  var $xml = "";
  var $domain =  "";

  function Performancing ($user, $pass, $domain) {
    return $this->auth ($user, $pass, $domain);
  } /* constructor */

  function auth ($user,$pass,$domain) {
    $this->domain = $domain;
    $this->url  = $this->base . "?action=getauth&uid=";
    $this->url .= urlencode ($user);
    $this->url .= "&pwd=" . urlencode ($pass);
    $this->xml = file_get_contents ($this->url);
    $domdoc = new DOMDocument ($this->xml);
    $node = $this->findTag ($domdoc, 'auth');
    $this->key = $node->get_content ();
    return 0;
  } /* auth */

  function getToppost () {
    $n = 0;
    $lastmonth = date("Y-m-d", mktime(0, 0, 0,
      date("m")-1, date("d"),  date("Y")));
    $today = $today = date("Y-m-d");

    $this->url  = $this->base . "?action=getpoststats&auth=";
    $this->url .= urlencode($this->key);
    $this->url .= "&blog_domain=" . urlencode($this->domain);
    $this->url .= "&m=date_range&d1=" . urlencode($lastmonth);
    $this->url .= "&d2=" . urlencode($today);

    $this->xml = file_get_contents ($this->url);
    $domdoc = new DOMDocument ($this->xml);

    /* recurse through document */
    if ($domdoc->has_child_nodes()) {
      $resp = $domdoc->child_nodes();
      $children = $resp[0]->child_nodes();
      foreach($children as $child) {
        if ($child->type == XML_ELEMENT_NODE) {
           if ($child->tagname() == 'Page') {
             $temp = $this->findTag($child, 'Title');
             if ($temp->get_content() != "Other") {
               $results[$n]['Title'] = $temp->get_content();
               $temp = $this->findTag($child, 'URL');
               $results[$n]['URL'] = $temp->get_content();
               $temp = $this->findTag($child, 'Views');
               $results[$n]['Views'] = $temp->get_content();
               $n++;
             }
           }
        }
      }
      return $results;
    } else {
      return "no children nodes found";
    }

    return "error finding results";

  } /* getToppost */

  /* accessor functions */
  function getUrl () {
    return $this->url;
  }

  function getKey () {
    return $this->key;
  }

  function getXml () {
    return $this->xml;
  }

  /* helper functions - take from php.net sample code */
  function findTag($node, $tag) {
       if($node -> has_child_nodes()) {
           $children = $node -> child_nodes();
           foreach($children as $child) {
               if($child -> type == XML_ELEMENT_NODE) {
                   if($child -> tagname() == $tag) {
                       return $child;
                   } else {
                       $a = $this->findTag($child, $tag);
                   }
               }
           }
           return $a;
       }
   }

} /* Performancing */

?>

Then you simply include this class in your PHP code, instantiate the class and call the necessary functions to generate the results. For example:

<?php
  require_once('performancing.class.php');
  $perf = new Performancing ('username', 'password', 'blog URL');
  $results = $perf->getToppost();

  foreach ($results as $result) { ?>
    <a href="<?=$result['URL']?>"><?=$result['Title']?></a>
    -<?=$result['Views']?><br />
<?php } ?>

The class still needs some work but I believe its a start. As you can see, it is still incomplete. Maybe given some more free time I can complete it. Performancing might not be as featureful as MeasureMap (now a Google company). But, it does provide some decent data that is relevant to majority of users.

Leave a Reply