3D Ohio Logo

Feedburner stats / XML Parsing

Here’s an example of how to add feedburner stats to your site. This example is also a good tutorial for beginners to understand parsing XML files / code with PHP. The coding example is pretty straight forward and commented.

The only prereq for this is that your blog is already signed up with Feedburner account. The blog should use a plugin like FeedSmith, but it technically doesn’t have to.

The first thing that we need to do is to make sure that the API is turned on for your blog. Login to feedburner as normal and go to your blog feed, then under Publicize, select Awareness API as shown:
Step One
and here:
Step Two
You can now go to the stat/XML document for your blog, which may look something like:

<rsp stat="ok">
  <!--This information is part of the FeedBurner Awareness API. If you want to
 hide this information, you may do so via your FeedBurner Account.-->
  <feed id="1115023" uri="3dOhio">
    <entry date="2007-08-20" circulation="1" hits="4"/>
  </feed>
</rsp>

Finally, we can access this information and parse it with the following code:

<?php
  $feedbunerURI = 'http://api.feedburner.com/awareness/1.0/GetFeedData?uri=';
  $myURI = '3dohio';
  $data = implode('', file($feedburnerURI.$myURI));
  $parser = xml_parser_create();
  // Use case-folding to make sure that we find the tag in XML code
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  // Use Skip White to remove the white spaces
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parse_into_struct($parser, $data, $values);
  // Free the XML parser
  xml_parser_free($parser);
   foreach ($values as $key => $tag) {
      // Find the entry tag with the attributes that we're looking for
      // This is a little over kill, but if the XML structure ever changes,
      //   this will still work
      if ($tag['tag'] == 'entry' && $tag['type'] == 'complete')
      {
          echo 'date: '        . $tag['attributes']['date']        . '<br />';
          echo 'circulation: ' . $tag['attributes']['circulation'] . '<br />';
          echo 'hits: '        . $tag['attributes']['hits']        . '<br />';
      }
  }
?>

The tricky part is understanding the structure of your array of XML tags. Here is the structure of the above code:

Array
(
    [0] => Array
        (
            [tag] => rsp
            [type] => open
            [level] => 1
            [attributes] => Array
                (
                    [stat] => ok
                )
        )
    [1] => Array
        (
            [tag] => feed
            [type] => open
            [level] => 2
            [attributes] => Array
                (
                    [id] => 1115023
                    [uri] => 3dOhio
                )
        )
    [2] => Array
        (
            [tag] => entry
            [type] => complete
            [level] => 3
            [attributes] => Array
                (
                    [date] => 2007-08-20
                    [circulation] => 1
                    [hits] => 4
                )
        )
    [3] => Array
        (
            [tag] => feed
            [type] => close
            [level] => 2
        )
    [4] => Array
        (
            [tag] => rsp
            [type] => close
            [level] => 1
        )
)

As you can see, you could directly address the second array item ($values[2]['attributes']['date']) but if something changes in the future, then your script will fail.

When you’re done with the code, you should have results similar to this:

date: 2007-08-20
circulation: 1
hits: 4

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Wordpress Feedburner Valid XHTML 1.0 Transitional Valid CSS!
copyright 2007 3D Ohio