Simple use of DIGG API for your blog

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I have recently put up a list of DIGG popular stories on my sidebar. The reason for this was the advent of the new DIGG API which gives access to a whole range of data from the DIGG database.

There are already some good PHP classes to access the API but I wanted some thing quick and simple that would not put a lot of requirements on my current hosting (PHP 4). The API gives three very good ways to receive the data (XML, JSON and serialized PHP).

I quickly discovered that XML decoding under PHP 4 is a pain in the (****) when you do not have access to install further modules. So quickly moved onto JSON, but the lovely function json_decode is PHP 5 only. But I did a bit of search and found several PHP json classes without any dependencies. I had also ruled out the PHP serialised format as it tries to unserialise into classes that I do not have defined (again dependencies.)

I then added some caching as I really didnt want to upset the lovely people at DIGG with constant requests when the data was not likely to change much.

You can download the project (its only 2 files.) from the code bank area. It is not pretty code, and it’s not encapsulated in a class or anything as I did it purely for speed (of coding, and execution.)

Below are a few of the important sections.

$response = file_get_contents("http://services.digg.com/stories/container/
	$category/popular?appkey=$appkey&count=$count&type=json");
// create an instance of the JSON service
$json = new Services_JSON();
// Decode the JSON we requested
$decoded = $json->decode($response);

The URL is built up to retrieve only the popular stories from a specific category. (technology I have shown on my sidebar.) I then use the enclosed JSON decoder to convert that into a PHP array.

echo "
<ul>";
// Output the stories
foreach($decoded->stories as $story) {
	$title = $story->title;
	if($title != '') {
		if(strlen($title) > 30) {
			$title = substr($title,0,29).'...';
		}
		echo $title;
	}
}
echo "</ul>";



This iterates through the array and displays each in an unordered list. There is a lot more information stored within each of the stories but all I am displaying is the link.

And before someone comments that this could all be done via RSS, I agree, but I can easily now adapt what I have got to retrieve a whole range of different interesting lists.

Some ideas for what else to do with the DIGG API

  • Extract out the users who have posted the popular stories
  • Show the time difference between posting and the story becoming popular
  • build up trends of the most popular posters
  • Create custom tag clouds from the words supplied in the stories

There Are 12 Responses So Far. »

  1. Sounds cool. I wonder if there’s anything for Perl or RoR to play with the Digg API

  2. I have not heard as yet of a specific Perl or Ruby API but anything that can perform an HTTP request and decode XML or JSON has the ability to work with the API. The fact that there is already a lot of PHP support is purely because DIGG is written in PHP and their developers I am sure just extracted their own internal code.

  3. Nick Halstead’s Blog: Simple use of DIGG API for your blog…

  4. […] started playing with it to integrate it with their own applications. On developer, Nick Halstead, decided to grab the latest stories and publish the headlines to his page. In the process, though, he learned a little something about […]

  5. […] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]

  6. […] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]

  7. […] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]

  8. Hey, I thought this was quite awesome, so I included it in my top ten list for digg apis. Nice work.

  9. Thanks for including me in your list Brian!

  10. Thank you sooooo much!

  11. […] read more | digg story […]

  12. […] There’s a good geek describtion about “using the Digg API”, which I partly used. As an alternative, JSON in PHP is also possible. […]

Post a Response