» Get an article's Digg count in PHP

If you're like me and you're interested in the stats of your website or blog, you might also want to know how many Diggs all of your articles have received. But that can become quite a pain when you have more and more articles, pages or blog posts. So why not let PHP retrieve the Digg count of your articles so you can use it in graphs or other statistic tools?

Exploring my options

  1. At first I thought I would need the Digg API (Services_Digg from PEAR) but I think it wasn't really meant to do that, it better suitable for retrieving lists of Digg posts. You first need to know where Digg has stored your article, yet all we have is our own URL.
  2. Then I thought why not use Javascript to get the innerHTML property of the Digg Counter object that's on each article and then send it to my server just like Google Analytics does. But then I saw it was actually an IFRAME, so cross site scripting limitations prevent that.
  3. But since the counter was hosted in that IFRAME, why not just copy it's source URL, and download & parse it every once in a while?

So that's going to be the approach then!

Prerequisites

We need to be able to download with PHP. Something that automatically handles redirections (cause Digg will forward you to the correct location). You could use curl or Snoopy for that, but I'm going to use the HTTP_Client class from PEAR. Why? Because PEAR rocks, it's really easy to setup and keep your classes up to date.

Install HTTP_Client

If you use Ubuntu like me, open a terminal to your webserver and type:

sudo aptitude -y update && aptitude install php-pear # installs pear
sudo pear install HTTP_Client # installs the HTTP_Client class

Done!
If you do not have Ubuntu, use another package manager to install PEAR, or just download everything you need at pear.php.net. It's all just PHP code, so no need to compile or anything.

OK Let's code!

We will store our code in a function so that we can easily reuse it later.

function diggCountByUrl($document_url){
  // include our prerequisites
  require_once "PEAR.php";
  require_once "HTTP/Client.php";
  
  // download the yellow Digg counter of the $document_url
  $url = "http://digg.com/tools/diggthis.php?u=".urlencode($document_url);
  $req =& new HTTP_Client(); 
  if (PEAR::isError($req->get($url))) return false;
  $response = $req->currentResponse();
  
  // parse the download so we only maintain the digg count
  preg_match('/<li.*?class="digg-count".*?>.+?\>(\d+)\<.+?<\/li>/si',$response["body"],$matches);
  return (is_numeric($matches[1])?$matches[1]:false);
}

That's it! In only 8 lines of code!

How to use the function - An Example

Now that we have the function, and the prerequisites, this is how we could use it:

// the url you want the Digg count from
$article_url = "http://kevin.vanzonneveld.net/techblog/article/get_an_articles_digg_count_in_php/";
$article_id  = 7;
if (($digg_cnt = diggCountByUrl($article_url)) !== false){
  // if all went well, $digg_cnt will not be false, 
  // and we can store the Digg count in a table for 
  // later use
  mysql_query("
    UPDATE `blog_article` 
    SET `digg_count` = ".$digg_cnt." 
    WHERE `blog_article_id`=".$article_id." 
    LIMIT 1
  ");
}

Like this, we could run the Digg poller every once in a while, because the results are stored in our database. It enables us to order articles based on Diggs, plot graphs of Diggs, etc. Neat.

The code above could use a little more error handling and stuff, but this is the basic idea to get you started!

Remember!

Digg is our friend and we do not want to bother them with more requests then strictly necessary. So do not to include this code in your normal pages (you might also get banned), but use the Linux Crontab to schedule jobs every once in a while.

Any comments?

I'm always open to suggestions and I will update the article with usefull comments.


Like this article?

   Then Dzone it!
Or use another bookmark button below to show your support &
help me spread the word.


tags: php, programming, social bookmarking, blog, PEAR, digg, javascript, url
category: Programming - PHP
read: 3,096 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

#5. Kevin (link) on 15 March 2008

Member avatar: KevinDejan: It certainly is!

#4. Dejan on 14 March 2008

Default avatar:Dejanhave you tried this, is it working..?

#3. Daniel Skinner on 01 February 2008

Default avatar:Daniel SkinnerYeah it's a shame you cannot search using the Digg API by URL.

Thanks for this :)

#2. Kevin (link) on 13 September 2007

Member avatar: Kevin@ Tim Broder: Yes I've looked into that but as far as I could tell, I believe that this:
1. is faster
2. doesn't require you to know the Digg URL to get the Digg count. e.g., you can supply
this URL: http://kevin.vanzonneveld.net/techblog/article/get_an_articles_digg_count_in_php/
instead of: http://digg.com/programming/How_To_Get_Digg_Count_in_PHP

#1. Tim Broder on 13 September 2007

Default avatar:Tim BroderWhy not just use the digg api?