How many Diggs? Intro to Using the Digg API

So I ran into this person the other day on the street…they looked at me and remarked

Why Terri, you haven’t posted in quite a while. I’ve been patiently awaiting your follow up to Start Using Delicious and FeedBurner API, Quick and Easy! I need to know how to display how many Diggs, Delicious bookmarks and Technorati reactions people have to each individual post!

So maybe I exaggerated a little. No one said that to me.

Oddly enough I don’t get recognized on the street for being a blogger, shocking ain’t it? Anyhow, are you ready? I’m about to give you the PHP functions you need to quickly determine how many Diggs, Delicious bookmarks and Technorati reactions any page has in a quick three part mini-series about APIs and using them with common social bookmarking sites.

WARNING

All of the following use a PHP5+ function: simplexml_load_file() PHP.net docs if you are on PHP4 you’re on your own as far as reading XML is concerned. I have switched almost all my environments to PHP5 specifically to have access to this spectacular function.

Find the Number of Diggs a Page Has Received

aka. Diggn’ it just a little bit?

This was an easy one. It’s pretty easy to determine how many Diggs a specific page has received. You can read more about Digg’s API But just by editing one line, you too can be a proud owner of a Digg count API querying function.

Below is the function. You pass the page you want to check on as a parameter or it defaults to the page the browser is requesting if you don’t pass it anything. The API is queried using the simplexml_load_file() function and passes the encoded URL with the type of response we want(xml) and an application key. At this point Digg recommends your application key be the url of your script or application. I have the function to default the application key to the root of the domain the function is running on. If you wish to change that, replace urlencode('http://'.$_SERVER['HTTP_HOST']) with urlencode('URL OF YOUR CHOICE').

REQUIRED You must set the user_agent when you call the Digg API, You should change the first line of the function, where I have ‘CHANGE ME TO NAME YOUR APPLICATION‘ to something to describe your application. I use something like: ‘Ninedays.org Reaction Display/1.0.’

PHP

<?php
    function get_digg_count($page=NULL){
        ini_set('user_agent', 'CHANGE ME TO NAME YOUR APPLICATION'); # Change this!
        $page = empty($page) ? 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : $page;
        $apicall = 'http://services.digg.com/stories/';
        $apicall .= '?link='.urlencode($page);
        $apicall .= '&appkey='.urlencode('http://'.$_SERVER['HTTP_HOST']);
        $apicall .= '&type=xml';
        $result = (array) simplexml_load_file($apicall);
        if(!empty($result['story'])){
            $result['story'] = (array) $result['story'];
            return (!empty($result['story']['@attributes']['diggs']))? intval($result['story']['@attributes']['diggs']) : 0;
        } else {
            return 0;
        }
    }
?>

The function returns the response so you can echo it, or use it to manipulate your script. I chose to have it return the response so you would only need to call and store it’s value once no matter how many times you need to use the returned value. Remember, every time you call the function, you query the API and as discussed below: you don’t want to do that too much and get blocked!

Here are few examples using the function:

<?php
    # Queries the current page by default since no parameter url is passed
    echo 'This page has ', get_digg_count(),' diggs!';

    # Uses the returned value in a decision
    if(get_digg_count('http://ninedays.org/') > 0){
        echo 'That other page has been dugg more than once, display digg link here?!?!?!';
    }

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

Super easy to use and since you are querying Digg’s public API not only is it polite but also strongly recommended that you cache your results. You can find plenty of information on the web about caching with PHP.

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 your Diggs start rolling in!

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 2 comments }

1 AzAkers June 13, 2009 at 2:53 am

After a whole lot of searching yours is the first tutorial/walk-through on this that I could successfully implement & test out. Its working great, and I’m backtracking into your code and learning more about how it was accomplished.

I’m looking into that ‘caching’ bit next so I don’t over-query Digg.

Thanks for sharing this and making it so simple that even I could understand :)

2 Levi Page October 7, 2009 at 6:11 pm

Is there a way to get the number of diggs for an entire domain rather than a single page?

Thanks,
Levi

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: