How many Diggs? Intro to Using the Digg API


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

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!

Series Navigation

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
MySQL Queries Made Easy With PHP Functions Library
This entry is part 1 of 3 in the series APIs and PHPAPIs and PHP Series IndexStart Using Delicious and FeedBurner API, Quick and Easy!How many Diggs? Intro to Using the Digg APITechnorati Reaction Counter API FunctionEvery web developer has a library of code that they reference frequently, if not constantly. I'm sharing with
Start Using Delicious and FeedBurner API, Quick and Easy!
This entry is part 1 of 3 in the series APIs and PHPAPIs and PHP Series IndexStart Using Delicious and FeedBurner API, Quick and Easy!How many Diggs? Intro to Using the Digg APITechnorati Reaction Counter API FunctionUsing the FeedBurner and del.icio.us API's and feeds you can add the number of FeedBurner subscribers or del.icio.us bookmarks
From Query String to Cookie with JavaScript
This entry is part 1 of 3 in the series APIs and PHPAPIs and PHP Series IndexStart Using Delicious and FeedBurner API, Quick and Easy!How many Diggs? Intro to Using the Digg APITechnorati Reaction Counter API FunctionTake a query string and save the values to a cookie using JavaScript. A perfect solution for tracking query
Path and Directory Function in PHP
This entry is part 1 of 3 in the series APIs and PHPAPIs and PHP Series IndexStart Using Delicious and FeedBurner API, Quick and Easy!How many Diggs? Intro to Using the Digg APITechnorati Reaction Counter API FunctionSome great handy PHP functions for dissecting file paths and getting full paths for how they relate to the
Technorati Reaction Counter API Function
This entry is part 1 of 3 in the series APIs and PHPAPIs and PHP Series IndexStart Using Delicious and FeedBurner API, Quick and Easy!How many Diggs? Intro to Using the Digg APITechnorati Reaction Counter API FunctionA simple PHP function to help you query the Technorati API and find out how many reactions Technorati has
Next Post
Technorati Reaction Counter API Function
Previous Post
Start Using Delicious and FeedBurner API, Quick and Easy!

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!