Technorati Reaction Counter API Function

by Terri Ann on August 4, 2008

This entry is part 3 of 3 in the series APIs and PHP

It’s very easy to track how many Technorati reactions a page has using the Technorati API.

Before you can use the Technorati API you’ll need to not only sign up with Technorati, but also get a developers key. The directions for getting a developer key can be found in Technorati’s API documentation.

Once you have your Technorati API key, set it as a constant using the PHP define() function. By setting it as a constant it makes the variable global in scope, meaning I can use it within a function without passing it as a parameter or defining it as global.

I’ll add the following line, usually to a configuration file so I have access to it when necessary. Just replace the xxxx’s with the API key provided from Technorati.

PHP

<?php
    define('TECHNORATI_KEY','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
?>

Just like the Digg count PHP function we built the other day the Technorati reactions function will default to the browser’s current page if a URL isn’t passed as a parameter. Other than that it’s just about plug-and-play.

PHP

<?php
    function get_technorati_reactions($page=NULL){
        $page = empty($page) ? 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : $page;

        $apicall = 'http://api.technorati.com/cosmos';
        $apicall .= '?key='.TECHNORATI_KEY;
        $apicall .= '&url='.urlencode($page);
        $apicall .= '&format=xml';
        $result = simplexml_load_file($apicall);

        return intval($result->document->result->inboundlinks);
    }
?>

Here are few examples using the function:

PHP

<?php   
    # Queries the current page by default since no parameter url is passed
    echo 'This page has gotten ', get_technorati_reactions(),' reactions seen by Technorati!';

    # Uses the returned value in a decision
    if(get_technorati_reactions('http://ninedays.org/') > 0){
        echo 'That other page has been reacted to more than once as seen on Technorati';
    }

    # Reusing the value
    $count = get_technorati_reactions('http://ninedays.org/portfolio/');
    echo 'My potfolio has ', $count ,' reactions, oh yes, a whole **',$count,'** reactions! I\'m psyched';
?>

Super easy to use and since you are querying Technorati’s public API you should definitely be caching your results. Most API’s restrict the number of times you can query the API and those that don’t most likely will at some point. The best way to avoid being banned or black listed (and to help you page load faster) is server side caching!

My personal favorite article on the subject of caching with PHP is on I Love Jack Daniels.com Added Bytes – Caching with PHP. If you are using WordPress or another blog tool, check out caching plugins. Caching is not just good to key you API query count low but also to safeguard your server and site when people really start reacting to your posts!

{ 1 comment… read it below or add one }

1 Alex Sysoef August 14, 2008 at 7:17 am

Terri Ann,

You have posted on “Organize Series” author blog that you have a fix for 2.6 and his plugin. Any chance you could share it, please? I got a few series’s on my blog that are now broken and would greatly appreciate the fix!

Sincerely, Alex Sysoef

Leave a Comment

Previous post:

Next post: