splitbrain.org

electronic brain surgery since 2001

Conditional Digg Button

Digg Badges Digg is a great site to waste time but it can also drive lots of traffic to your site, making it known to many people at once1). In case you have been living under a rock for the last few years, here is what Wikipedia writes about Digg:

Digg is a community-based popularity website with an emphasis on technology and science articles. It combines social bookmarking, blogging, and syndication with a form of non-hierarchical, democratic editorial control. News stories and websites are submitted by users, and then promoted to the front page through a user-based ranking system.

The problem is that visitor coming from Digg may not go back there to “digg it”, which is what you want to get promoted to Digg's frontpage. The solution is simple: adding a small reminder at your page. Digg offers a simple JavaScript snippet to display a badge with the number of current diggs and nice “digg it” link. Very easy and user friendly.

Unfortunately this script needs to be seeded with a “Digg story URL”. Of course this URL is only available after someone has submitted your page. But how can you know when one of your articles is submitted to Digg? Doing a manual checking is really not feasible – you can't spend your whole day on checking the Digg queue, can't you? 2).

Happily there is a solution. Digg has an undocumented, unoffical API3). However there is some 3rd Party documentation. This API can be used to query Digg for stories by using an URL like this:

 http://services.digg.com/stories?link=<mypageurl>

The result is an XML file with all stories matching the given link (if any) including the story URL at Digg. You now can use this info to call the JavaScript or add a regular submit link into your page.

The following PHP code should give you an idea how to do it4):

  $url   = 'http://example.com/my/blogpost';
  $title = 'My example post';
 
  $data = file_get_contents('http://services.digg.com/stories?link='.rawurlencode($url));
 
  if(preg_match('!<story.*?href="(http://digg.com.*?)"!i',$data,$match)){
    $diggurl = $match[1];
    echo '<iframe src="http://digg.com/api/diggthis.php?u='.rawurlencode($diggurl).
         '" height="80" width="52" frameborder="0" scrolling="no"></iframe>';
  }else{
    echo '<a href="http://digg.com/submit?phase=2&amp;url='.rawurlencode($url).'&amp;title='.rawurlencode($title).'">';
    echo '<img src="digg.gif" height="80" width="51" border="0" alt="" /></a>';
  }

You can see the result at the end of my blog articles. If you want to try it, feel free to digg me ;-).

Tags:
digg, php, api
Similar posts:
1)
If you survive the Digg effect
2)
Well, most of us can't – I guess there may be some freaks who do ;-)
3)
At least I couldn't find any info about it at Digg's page
4)
I directly include the iframe which is usually created by Digg's JavaScript